Java 使用WebDriver获取子元素

Java 使用WebDriver获取子元素,java,selenium-webdriver,Java,Selenium Webdriver,我有以下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-

我有以下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>
下面的
ul
如下所示:

WebElement ddChild = dropdowns.get(0).findElement(By.className("ui-selectmenu-menu-dropdown"));
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[@id='gwt-uid-191-menu']/li[*]"));
我甚至可以像这样抓取
ddChild
下的所有
li

WebElement ddChild = dropdowns.get(0).findElement(By.className("ui-selectmenu-menu-dropdown"));
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[@id='gwt-uid-191-menu']/li[*]"));
List ddOpts=ddChild.findElements(By.xpath(“//*[@id='gwt-uid-191-menu']/li[*]);
但问题是,我似乎不知道如何获取
列表ddOpts=ddChild.findElements的文本值(By.xpath(“/*[@id='gwt-uid-191-menu']/li/a”);
ArrayList links=新的ArrayList();
for(WebElement we:ddOpts){
links.add(we.getText();
}
列出ddOpts=ddChild.findElements(By.xpath(“/*[@id='gwt-uid-191-menu']/li/a”);
ArrayList links=新的ArrayList();
for(WebElement we:ddOpts){
links.add(we.getText();
}

要提取
WebElement
href
属性(参考本例中的锚定标记
,请执行以下操作:

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"));

}
List ddOpts=ddChild.findElements(By.xpath(“/*[@id='gwt-uid-191-menu']/li/a”);
ArrayList links=新的ArrayList();
for(WebElement we:ddOpts){
//将所有href属性字符串添加到列表中
links.add(we.getAttribute(“href”);
}

要提取
WebElement
href
属性(参考本例中的锚定标记
,请执行以下操作:

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"));

}
List ddOpts=ddChild.findElements(By.xpath(“/*[@id='gwt-uid-191-menu']/li/a”);
ArrayList links=新的ArrayList();
for(WebElement we:ddOpts){
//将所有href属性字符串添加到列表中
links.add(we.getAttribute(“href”);
}

这也可以解决您的问题:

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;
    }
}
List下拉列表=driver.findElements(By.className(“x-combo-List”);
WebElement ddChild=dropdowns.get(0.findElement)(By.className(“x-combo-list-inner”);
列出ddOpts=ddChild.findElements(By.xpath(“/*[@id=\“x-auto-98\”]/div[4]”);
for(WebElement we:ddOpts){
System.out.println(we.getText());
if(we.getText()包含(“角色\管理器”)){
we.sendKeys(“角色经理”);
we.click();
打破
}
}

这也可以解决您的问题:

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;
    }
}
List下拉列表=driver.findElements(By.className(“x-combo-List”);
WebElement ddChild=dropdowns.get(0.findElement)(By.className(“x-combo-list-inner”);
列出ddOpts=ddChild.findElements(By.xpath(“/*[@id=\“x-auto-98\”]/div[4]”);
for(WebElement we:ddOpts){
System.out.println(we.getText());
if(we.getText()包含(“角色\管理器”)){
we.sendKeys(“角色经理”);
we.click();
打破
}
}
href=“#nogo”
对于所有锚定标记都是相同的,因此在通过该方法选择项目时可能会产生歧义

dropdowns.findelement(By.linktext("#nogo"));
href=“#nogo”
对于所有锚定标记都是相同的,因此在通过该方法选择项目时可能会产生歧义

dropdowns.findelement(By.linktext("#nogo"));

下面的代码将在上面的HTML代码下拉列表中选择选项PHA

findElement(By.xpath(“//*[@class='ui-selectmenu-menu'))。单击()


driver.findelelement(By.xpath(“//*[@class='ui-widget-ui-widget-ui-widget-ui-widget-content-ui-selectmenu-menu-dropdown-ui-corner-bottom']/**[text()='Option-Alpha'])。单击();

下面的代码将在上面的HTML代码的下拉列表中选择OptionAlpha

findElement(By.xpath(“//*[@class='ui-selectmenu-menu'))。单击()


driver.findElement(By.xpath(“/*[@class='ui-widget-ui-widget-widget-content-ui-selectmenu下拉菜单ui-corner-bottom']//**[text()=“Option Alpha']”)。单击();

请尝试以下代码获取
中的所有链接请尝试以下代码获取
中的所有链接是否尝试使用.getCssValue(“href”)li元素的值?是否尝试使用li元素的.getCssValue(“href”)值?