【问题标题】:Getting child elements using WebDriver使用 WebDriver 获取子元素
【发布时间】:2022-04-25 01:02:10
【问题描述】:

我有以下 HTML 代码:

<div class="ui-selectmenu-menu" style="z-index: 1; top: 251px; left: 37px;">
    <ul class="ui-widget ui-widget-content ui-selectmenu-menu-dropdown ui-corner-bottom" aria-hidden="true" role="listbox" aria-labelledby="gwt-uid-191-button" id="gwt-uid-191-menu" style="width: 270px; height: auto;" aria-disabled="false" aria-activedescendant="ui-selectmenu-item-999">
        <li role="presentation" class="ui-selectmenu-item-selected">
          <a href="#nogo" tabindex="-1" role="option" aria-selected="true" id="ui-selectmenu-item-999">All Applications</a></li>
        <li role="presentation" class="">
          <a href="#nogo" tabindex="-1" role="option" aria-selected="false">Option Alpha</a></li>
        <li role="presentation" class="ui-corner-bottom">
          <a href="#nogo" tabindex="-1" role="option" aria-selected="false">Option Beta</a></li>
    </ul>
</div>
...
<div class="ui-selectmenu-menu"...>...</div>

我可以像这样获取ui-selectmenu-menu 的WebElement(页面上有很多;因此,使用findElements):

List<WebElement> dropdowns = driver.findElements(By.className("ui-selectmenu-menu"));

下面的ul是这样的:

WebElement ddChild = dropdowns.get(0).findElement(By.className("ui-selectmenu-menu-dropdown"));

我什至可以像这样抓取ddChild 下的所有li

List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[@id='gwt-uid-191-menu']/li[*]"));

但问题是我似乎无法弄清楚如何在每个 li 元素下获取 &lt;a href="#nogo"... 标记的文本值。

我希望能够遍历所有 ddOpts 并获取 &lt;a href="#nogo"... 文本值并将它们保存到 ArrayList&lt;String&gt;

因此,例如,我的第一个ArrayList&lt;String&gt; 值将包含All Applications,然后是Option Alpha,然后是Option Beta,然后从下一个dropdowns 跳转到下一个ul 元素并执行整个再次处理,同时添加到ArrayList&lt;String&gt;

我确信它是一个简单的解决方案,但我对 Selenium WebDriver 的经验有限。

谢谢!

PS:有没有一种简单的方法来获取 WebElement 的子元素?

【问题讨论】:

  • 您是否尝试过使用 li 元素的 .getCssValue("href") 值?

标签: java selenium-webdriver


【解决方案1】:
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[@id='gwt-uid-191-menu']/li/a"));
ArrayList<String> links = new ArrayList<String>();
for(WebElement we : ddOpts) {
    links.add(we.getText();
}

【讨论】:

    【解决方案2】:

    要提取WebElementhref 属性(参考本例中的锚标记&lt;a&gt;,请执行以下操作:

    List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[@id='gwt-uid-191-menu']/li/a"));
    ArrayList<String> links = new ArrayList<String>();
    for(WebElement we : ddOpts) {
    
        // ADD all the href attribute strings to the list
        links.add(we.getAttribute("href"));
    
    }
    

    【讨论】:

      【解决方案3】:

      这也可能解决您的问题:

      List<WebElement> dropdowns = driver.findElements(By.className("x-combo-list"));        
      WebElement ddChild = dropdowns.get(0).findElement(By.className("x-combo-list-inner"));   
      List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[@id=\"x-auto-98\"]/div[4]"));
      
      
      for(WebElement we:ddOpts){
          System.out.println(we.getText());
          if(we.getText().contains("ROLE_MANAGER")){
      
              we.sendKeys("ROLE_MANAGER");
              we.click();
              break;
          }
      }
      

      【讨论】:

        【解决方案4】:

        下面的代码将在上面的 HTML 代码的下拉列表中选择 OptionAlpha

        driver.findElement(By.xpath("//*[@class='ui-selectmenu-menu')).click();

        driver.findElement(By.xpath("//*[@class='ui-widget ui-widget-content ui-selectmenu-menu-dropdown ui-corner-bottom']//**[text() ='Option Alpha']")).click();

        【讨论】:

          【解决方案5】:

          请尝试以下代码获取&lt;a href中的所有链接

          List<WebElement> allLis = driver.findElements(By.xpath("//*[@id='gwt-uid-191-menu']/li/a");
          // Looping through above list using for-each loop
          for(WebElement eachLi : allLis) {
              System.out.println(eachLi.getText());
          }
          

          希望这会有所帮助。

          【讨论】:

            【解决方案6】:

            href="#nogo"对于所有锚标签都是相同的,因此在通过该方法选择项目时可能会产生歧义

            dropdowns.findelement(By.linktext("#nogo"));
            

            【讨论】:

              猜你喜欢
              • 2013-03-09
              • 1970-01-01
              • 1970-01-01
              • 2021-10-14
              • 2016-05-02
              • 1970-01-01
              • 2021-07-04
              • 1970-01-01
              • 2021-10-07
              相关资源
              最近更新 更多