Java 使用CSS选择器查找关系复杂的Web元素

Java 使用CSS选择器查找关系复杂的Web元素,java,selenium,css-selectors,Java,Selenium,Css Selectors,我正试图根据相邻文本单击复选框。然而,页面结构使其变得相当困难。问题是事物嵌套在div等嵌套的表中。所讨论的代码部分如下所示: if(div has text of "Ability to Add/remove Queues"){ return the WebElement of the check box associated with it } boolean found = false; for(String id : idList) { WebElement el =

我正试图根据相邻文本单击复选框。然而,页面结构使其变得相当困难。问题是事物嵌套在div等嵌套的表中。所讨论的代码部分如下所示:

if(div has text of "Ability to Add/remove Queues"){
    return the WebElement of the check box associated with it
}
boolean found = false;
for(String id : idList) {
    WebElement el = driver.findElement(By.xpath("//input[@id='"+id+"']/../../following-sibling::td[1]/div[@class='yui-dt-liner']"));
    if (el.getText().indexOf("Ability to Add/remove Queues") != -1) {
        found = true;
        break;
    }
}
// do something with `found`
WebElement input = driver.findElement(By.xpath(
    "//div[contains(text(),'Ability to Add/Remove Queues')]"+
    "/../preceding-sibling::td[1]"+
    "//input[@name='functionId']"
));

添加/删除队列的能力
我需要能够通过ID为
functionId13
的输入找到包含添加/删除队列的
功能的div。这将导致如下检查:

if(div has text of "Ability to Add/remove Queues"){
    return the WebElement of the check box associated with it
}
boolean found = false;
for(String id : idList) {
    WebElement el = driver.findElement(By.xpath("//input[@id='"+id+"']/../../following-sibling::td[1]/div[@class='yui-dt-liner']"));
    if (el.getText().indexOf("Ability to Add/remove Queues") != -1) {
        found = true;
        break;
    }
}
// do something with `found`
WebElement input = driver.findElement(By.xpath(
    "//div[contains(text(),'Ability to Add/Remove Queues')]"+
    "/../preceding-sibling::td[1]"+
    "//input[@name='functionId']"
));
我有一个所有复选框及其ID的列表,我只是想不出一种方法来将它们与它们的描述性文本相匹配。

有,如果要选择此选项,您需要一个复选框,但您可以使用XPath:

//input[@id='functionId13']/../../following-sibling::td[1]/div[@class='yui-dt-liner']
  • //输入[@id='functionId13']
    选择输入
  • /../..//code>上升到第一个td
  • /following sibling::td[1]
    在当前节点之后给出td
  • 并且
    /div[@class='yui-dt-liner']
    选择第二个td中的div
  • 在java中,它将如下所示:

    if(div has text of "Ability to Add/remove Queues"){
        return the WebElement of the check box associated with it
    }
    
    boolean found = false;
    for(String id : idList) {
        WebElement el = driver.findElement(By.xpath("//input[@id='"+id+"']/../../following-sibling::td[1]/div[@class='yui-dt-liner']"));
        if (el.getText().indexOf("Ability to Add/remove Queues") != -1) {
            found = true;
            break;
        }
    }
    // do something with `found`
    
    WebElement input = driver.findElement(By.xpath(
        "//div[contains(text(),'Ability to Add/Remove Queues')]"+
        "/../preceding-sibling::td[1]"+
        "//input[@name='functionId']"
    ));
    
    用于向后执行的XPath如下所示:

    if(div has text of "Ability to Add/remove Queues"){
        return the WebElement of the check box associated with it
    }
    
    boolean found = false;
    for(String id : idList) {
        WebElement el = driver.findElement(By.xpath("//input[@id='"+id+"']/../../following-sibling::td[1]/div[@class='yui-dt-liner']"));
        if (el.getText().indexOf("Ability to Add/remove Queues") != -1) {
            found = true;
            break;
        }
    }
    // do something with `found`
    
    WebElement input = driver.findElement(By.xpath(
        "//div[contains(text(),'Ability to Add/Remove Queues')]"+
        "/../preceding-sibling::td[1]"+
        "//input[@name='functionId']"
    ));
    

    有了CSS选择器,您将无法获得它,因为它不支持这种需求

    唯一简单的方法是使用XPath,如下所示

    WebElement yourElement = driver.findElement(By.xpath("//div[contains(text(),'Ability to Add/Remove Queues')]");
    

    或者,您可以使用findElements()获取所有div并循环它们以查找包含所需文本的元素。

    您用什么语言编写此代码?XPath应该能够做到这一点。我不能硬编码函数ID13,因为它是动态的。我想我几乎需要做相反的事情,但是使用div的文本作为起点,因为这是我唯一可以确定的。@aweeks你说过你有一个复选框列表和它们的ID。迭代ID并检查通过ID找到的返回div是否与文本匹配。@aweeks我添加了一个迭代示例和元素的反向遍历。我曾想过按类获取所有div,但页面可以包含300个以上的div,并且只会随着时间的推移而增加(每个动态生成的东西都会得到一个带有“yui dt liner”类的div)。