【问题标题】:In Selenium IDE how can you select an option by index in a dropdown menu?在 Selenium IDE 中,如何在下拉菜单中按索引选择选项?
【发布时间】:2017-02-10 19:54:13
【问题描述】:

问题是选项标签改变了,所以我不能按标签抓取。我需要通过说选项[0]来抓取

有什么想法吗?

我正在使用 Selenium IDE (Firefox),这是我要询问的部分:

<tr>
    <td>select</td>
    <td>dateRangeString</td>
    <td>index=1</td>
</tr>

IDE中最后一个TD是VALUE字段,

我已经针对选择元素,但我需要模拟用户选择第一个选项。我看到这样做的唯一方法是在 IDE 的 VALUE 部分中使用 LABEL="string",但字符串是动态的,所以这不起作用!

【问题讨论】:

    标签: select selenium


    【解决方案1】:

    您可以使用 XPath //select/option[index] 选择它。

    请记住,按照标准,XPath 是基于 1 的索引。

    问题更新后编辑

    您可以使用多种不同的方式从一个选项中选择一个选项。下面是从 Selenium IDE 复制的关于如何创建选项定位器的内容。要选择第一项,它将是 index=0

    选择(选择定位器,选项定位器) 参数:

        * selectLocator - an element locator identifying a drop-down menu
        * optionLocator - an option locator (a label by default)
    
    Select an option from a drop-down using an option locator.
    
    Option locators provide different ways of specifying options of an HTML
    

    选择元素(例如,用于选择一个 特定选项,或断言 选定的选项满足 规格)。有几个 选择选项定位器的形式。

        * label=labelPattern: matches options based on their labels, i.e. the visible text. (This is the default.)
              o label=regexp:^[Oo]ther
        * value=valuePattern: matches options based on their values.
              o value=other
        * id=id: matches options based on their ids.
              o id=option1
        * index=index: matches an option based on its index (offset from zero).
              o index=2
    
    If no option locator prefix is provided, the default behaviour is to
    

    标签匹配。

    【讨论】:

    • 我认为你误解了我的需求。我已经瞄准了选择元素,但我需要模拟用户选择第一个选项。我看到这样做的唯一方法是在 IDE 的 VALUE 部分中使用 LABEL="label string"
    • 是的,在您发布更新之前我就知道如何使用 index=index =] 我最初有,但它似乎没有用。我不得不做一些等待命令。但感谢您提供完整的信息。你知道在哪里可以找到这个地方的文档吗?
    【解决方案2】:

    尝试使用 index="1"(选择第一个元素),而不是使用 label=""。 Selenium 为这种情况提供了 label=""、id=""、value="" 和 index=""。更多详情,请参见:

    http://release.seleniumhq.org/selenium-core/0.8.0/reference.html

    【讨论】:

      【解决方案3】:

      棘手,但“类型”命令在这里帮助了我:

      <tr>
          <td>type</td>
          <td>id=numStuff</td>
          <td>1</td>
      </tr>
      

      对应的html长这样(@id和@name属性除外)

      <select id="numStuff" name="numStuff">
                              <option selected= value="0">0</option>
                              <option value="1">1</option>
                              <option value="2">2</option>
                              <option value="3">3</option>
                              <option value="4">4</option>
                              <option value="5">5</option>
                              <option value="6">6</option>
                              <option value="7">7</option>
                              <option value="8">8</option>
                              <option value="9">9</option>
      </select>
      

      我想知道它是否适用于长于 1 次击键的选项值。至少,暂时还好。

      【讨论】:

        【解决方案4】:

        在 C# 中,我使用以下方法解决了这个问题: selenium.Select("id=yourID", "index="+i.ToString());

        【讨论】:

          【解决方案5】:

          在我使用的 selenium IDE 中:

          命令:选择
          目标:以 select 结尾的 XPath 目标(不是选项)。 XPath 可以通过 FireBug 和 FirePath 获得。
          值:例如“德克萨斯” 或您需要的任何值模式

          【讨论】:

            【解决方案6】:

            例如:

            select | Locator | index=1 |
            

            它对我有用。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-04-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-02-17
              相关资源
              最近更新 更多