英文:
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
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 < WebElement > elements = wbelement.findElements(drpdown);
System.out.println(elements.size());
System.out.println("Total Number of item count in dropdown list = " + elements);
for (int i = 0; i < elements.size(); i++) {
System.out.println(elements.get(i).getText());
System.out.println(elements.get(i).getAttribute("value")); //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, 'selection-search-input')]
Click that first and then you can build XPATH for dropdown values using Contains class like
//*[contains(@class, 'selection-search')]//*[contains(@class, 'selection-item')]
专注分享java语言的经验与见解,让所有开发者获益!
评论