【问题标题】:traverse nested tagnames - selenium automation using webDriver遍历嵌套标记名 - 使用 webDriver 的 selenium 自动化
【发布时间】:2012-07-05 19:20:23
【问题描述】:

朋友们,

我有一种情况需要单击下拉列表并选择显示的任何值。 下拉列表由以下代码标识

<select id="order_unit_line_rate_806782_is_addenda_enabled" class="selects_for_487886" onchange="select_addendum(806782, this);dateShowMemory(this.options[this.selectedIndex].value, '806782');" uniqueattr="Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / drop down" name="order_unit_line_rate[806782][is_addenda_enabled]">
    <option value="0" uniqueattr="Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / Fee"> Fee </option>
    <option value="1" uniqueattr="Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / See Attached Addendum"> See Attached Addendum </option>

在哪里 “选择”和 “选项”标签位于嵌套层次结构中。我可以单击下拉列表并通过这样做来显示项目

List<WebElement> dropDownLists = driver.findElements(By.tagName("select"));
for (WebElement l : dropDownLists) { 
    if (l.getAttribute("uniqueattr").equalsIgnoreCase("Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / drop down")) {
              l.click();
    } // end if
} // end for

但我无法进一步遍历以单击下拉列表中的选项:(。

这是我尝试过的,但不起作用

List<WebElement> newList = driver.findElements(By.tagName("option"));
for (WebElement ll : newList) {
    if (ll.getAttribute("uniqueattr").equalsIgnoreCase("Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / Straight Line Commitment")) {
        ll.click();
    }
}

【问题讨论】:

    标签: java selenium selenium-ide ui-automation selenium-firefoxdriver


    【解决方案1】:

    像这样检索你想要点击的选项的标签、索引或值

    List<WebElement> dropDownLists = driver.findElements(By.tagName("select"));
    for (WebElement l : dropDownLists) 
    { 
        List<WebElement> newList = l.findElements(By.tagName("option"));
        for (WebElement ll : newList) 
        {
                if (ll.getAttribute("uniqueattr").equalsIgnoreCase("Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / Straight Line Commitment")) 
                {
                    SelectElement select = new SelectElement(l);
                    select.SelectByValue(ll.getAttribute("value"));
                }
        }
    } 
    

    【讨论】:

      【解决方案2】:

      这就是Select 的用途..

      做一些类似的事情:

          Select dropDown = new Select(dropDownElement);
          for (WebElement option : dropDown .getOptions()){
              if(!option.isSelected()){
                  option.click();
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-08
        • 1970-01-01
        • 2016-06-26
        • 1970-01-01
        • 2016-12-06
        • 2019-12-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多