【问题标题】:Unable to select header/menu dropdown items using xpath/css traversing无法使用 xpath/css 遍历选择标题/菜单下拉项
【发布时间】: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


【解决方案1】:

您的代码的问题是parent.findElements(By.tagName("li")); 返回&lt;nav id="main-menu"... 下的所有li 元素。下面的代码有点冗长,希望对您有所帮助。

Grid.driver().get("https://testpt1qa.com");

// Get top level parent for all nav elements
WebElement parent = Grid.driver().findElement(By.xpath("//nav[@id='main-menu']//ul//li"));

// Get the main menu items: main-menu > ul > li > a
// At this point you should have Test1, Test2, Test3 and Help.
List<WebElement> menuItems = parent.findElements(By.xpath(".//a[@rel='menuitem'"));

// Get menu items size
System.out.println("Number of menu items " + menuItems.size());

// Get the text for each menu
for (WebElement menuItem : menuItems) {
    System.out.println(menuItem.getText());

    // Now search for sub menu item by traversing up to the parent of the current menu item
    // This should get you Get Class1 etc based on the current menu in the loop
    List<WebElement> subMenuItems = menuItem.findElements(By.xpath(".//..//div//ul//li//a"));
    System.out.println("Number of sub menu items " + subMenuItems.size());
    for (WebElement subMenuItem : subMenuItems) {
        System.out.println(subMenuItem.getText());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2020-09-15
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    相关资源
    最近更新 更多