如何使用Selenium WebDriver找到一个XPath,以返回下拉菜单的所有值。

huangapple 未分类评论50阅读模式
英文:

How to find a xpath which will return all values of dropdown using selenium webdriver

问题

在我的HTML代码中,没有<select>标签来定义选项,但是如果有一个下拉菜单(select dropdown),点击后会在HTML代码中显示下拉菜单项的名称。

[enter image description here][1]

  [1]: https://i.stack.imgur.com/cz3Oi.png
public void user_count_list_of_client_names() throws Exception {
    //分配并选择下拉列表元素
    wait.until(ExpectedConditions.visibilityOfElementLocated(drpdown));

    // driver.findElement(clientsdrpdown).click();
    WebElement wbelement = driver.findElement(drpdown);
    List<WebElement> elements = wbelement.findElements(drpdown);
    System.out.println(elements.size());

    System.out.println("下拉列表中的总项目数 = " + elements);

    for (int i = 0; i < elements.size(); i++) {
        System.out.println(elements.get(i).getText());
        System.out.println(elements.get(i).getAttribute("value")); //使用循环逐个获取下拉菜单名称,使用value属性。

        elements.get(i).click();
    }
}
英文:

In my HTML code don't have a tag of select either for options but if select dropdown then on click its shows name of a dropdown item on HTML code

enter image description here

     public void user_count_list_of_client_names() throws Exception {
        //Assign and Select the dropdown list element
        wait.until(ExpectedConditions.visibilityOfElementLocated(drpdown));

        // driver.findElement(clientsdrpdown).click();
        WebElement wbelement = driver.findElement(drpdown);
        List &lt; WebElement &gt; elements = wbelement.findElements(drpdown);
        System.out.println(elements.size());

        System.out.println(&quot;Total Number of item count in dropdown list = &quot; + elements);

        for (int i = 0; i &lt; elements.size(); i++) {
            System.out.println(elements.get(i).getText());
            System.out.println(elements.get(i).getAttribute(&quot;value&quot;)); //Using for loop getting one by one dropdown name using value attribute.

            elements.get(i).click();
        }
    }

答案1

得分: 0

所以,您的下拉字段xpath可以是这样的:

//input[contains(@class, 'selection-search-input')]

首先点击这个,然后您可以使用Contains类构建下拉值的XPATH,就像这样:

//*[contains(@class, 'selection-search')]//*[contains(@class, 'selection-item')]
英文:

So, your dropdown field xpath can be like

//input[contains(@class, &#39;selection-search-input&#39;)]

Click that first and then you can build XPATH for dropdown values using Contains class like

//*[contains(@class, &#39;selection-search&#39;)]//*[contains(@class, &#39;selection-item&#39;)]

huangapple
  • 本文由 发表于 2020年5月5日 18:47:29
  • 转载请务必保留本文链接:https://java.coder-hub.com/61611318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定