java 使用 WebDriver 获取子元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14449302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting child elements using WebDriver
提问by FilmiHero
I've got the following HTML code:
我有以下 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>
I'm able to get the WebElement for ui-selectmenu-menu
like this (there are many on the page; hence, the use of findElements
) :
我能够ui-selectmenu-menu
像这样获得 WebElement (页面上有很多;因此,使用findElements
):
List<WebElement> dropdowns = driver.findElements(By.className("ui-selectmenu-menu"));
And the ul
below it like this:
而ul
它下面这样:
WebElement ddChild = dropdowns.get(0).findElement(By.className("ui-selectmenu-menu-dropdown"));
I'm even able to grab all the li
under the ddChild
like this:
我什至可以像这样抓住所有li
的ddChild
东西:
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[@id='gwt-uid-191-menu']/li[*]"));
But the problem that I can't seem to figure out how to grab the text-value of the <a href="#nogo"...
tag under each li
element.
但是我似乎无法弄清楚如何获取<a href="#nogo"...
每个li
元素下标签的文本值的问题。
I'd like to be able to loop through all the ddOpts
and grab the <a href="#nogo"...
text values and save them to an ArrayList<String>
.
我希望能够遍历所有ddOpts
并获取<a href="#nogo"...
文本值并将它们保存到ArrayList<String>
.
So, for example, my first ArrayList<String>
value would contain All Applications
, then Option Alpha
, then Option Beta
, and then jump to the next ul
element from the next dropdowns
and do the whole process again, all while adding to the ArrayList<String>
.
因此,例如,我的第一个ArrayList<String>
值将包含All Applications
、 then Option Alpha
、 then Option Beta
,然后ul
从下一个元素跳转到下一个元素dropdowns
并再次执行整个过程,同时添加到ArrayList<String>
.
I'm sure its a simple solution but I've got limited experience with Selenium WebDriver.
我确定这是一个简单的解决方案,但我对 Selenium WebDriver 的经验有限。
Thanks!
谢谢!
PS: Is there a simple way to grab the child of a WebElement?
PS:有没有简单的方法来抓取 WebElement 的孩子?
回答by Aurand
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();
}
回答by Shivkumar Krishnan
To extract the href
attribute of the WebElement
(referring to the anchor tag <a>
in this example, do this:
要提取(参考本示例中的锚标记)的href
属性,请执行以下操作:WebElement
<a>
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"));
}
回答by user2431333
This may also solve your problem:
这也可以解决您的问题:
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;
}
}
回答by Balu
the below code will select the OptionAlpha in the dropdown of the above HTML code
下面的代码将在上面的 HTML 代码的下拉列表中选择 OptionAlpha
driver.findElement(By.xpath("//*[@class='ui-selectmenu-menu')).click();
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();
driver.findElement(By.xpath("//*[@class='ui-widget ui-widget-content ui-selectmenu-menu-dropdown ui-corner-bottom']//**[text()='Option Alpha']")).click();
回答by k.s. Karthik
Please try the below code to get all the links in the <a href
请尝试以下代码以获取所有链接 <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());
}
Hope this helps.
希望这可以帮助。
回答by Balu
href="#nogo"
is same for all the anchor tags, so it might create ambiguity in selecting the item by the method
href="#nogo"
对于所有的锚标签都是一样的,所以它可能会在通过方法选择项目时产生歧义
dropdowns.findelement(By.linktext("#nogo"));