【发布时间】:2015-01-03 14:56:03
【问题描述】:
我附上了正在测试的应用程序的示例 UI 图像。 我正在使用带有 java 的 webdriver 来获取标头值列表并获取下拉值。
我的问题 -
1) 使用 xpath,我无法获得尺寸和 ONLY 标头的文本值(Test1、Test2、test3、帮助)
2) 其次,Test1 具有下拉值,与 Test2 和 Test3 类似。 我应该能够获取 test1 的大小(应该是 4),然后根据 XPATH 位置获取文本值(获取 Class 1 等)。
代码
Grid.driver().get("https://testpt1qa.com");
// Get Size of the Navigation Header
WebElement parent = Grid.driver().findElement(By.xpath("//nav[@id='main-menu']"));
List<WebElement> child = parent.findElements(By.tagName("li"));
/**
* THIS IS listing all the links under Test1,Test2,Test3 & Help
*/
System.out.println(child.size());
for (WebElement webElement : child) {
String sizeval = webElement.getSize().toString();
System.out.println("********** " + sizeval);
....
....
示例 HTML 代码
<nav id="main-menu" role="navigation" style="background-color: transparent;">
<ul style="background-color: transparent;">
<li style="background-color: transparent;">
<a id="header-test1" class="selected" aria-controls="submenu-test1" rel="menuitem" href="https://testpt1qa.com" role="button" aria-expanded="true" style="background-color: transparent;">Test1</a>
<div id="submenu-test1" class="menu-wrapper on open done" aria-labelledby="header-test1" role="region" tabindex="-1">
<ul id="header-test1-menu" class="subnav list " style="background-color: transparent;">
<li style="background-color: transparent;">
<a href="https://testpt1qa.com/kp/helper5" style="background-color: transparent;">Get Class 1</a>
</li>
<li>
<a href="https://testpt1qa.com/kp/helper1" style="background-color: transparent;">Get Class 2</a>
</li>
<li>
<a href="https://testpt1qa.com/kp/helper2">Get Class 3</a>
</li>
<li>
<a href="https://testpt1qa.com/kp/helper3">Get Class 4</a>
</li>
<li>
</ul>
.....
.....
基于上面的代码,它打印所有下拉值和标题值的大小,而不是特定的。
【问题讨论】:
-
请提供 jsfiddle 以实时查看您的页面
-
我的页面只能在 corp 网络中使用,很抱歉,但我附上了一个示例 html sn-p。
标签: java selenium xpath selenium-webdriver