Selenium继续检查直到找到文本

时间:2017-02-21 06:40:33

标签: selenium selenium-webdriver

我必须找到一个特定的文字,我只能使用className标记下的span来完成。不幸的是,className对于其他各种span标记很常见。如果我尝试使用text查找className,则很有可能只会首次注册span tag ---> className

有什么方法可以继续检查整个页面,直到className--->text的最后一行?

我无法使用findElements()方法。这样做会涉及代码中的很多变化,因为这只是一次性的事情,我不想经历所有的麻烦。

1 个答案:

答案 0 :(得分:1)

我使用以下方法使用文本查找元素。如果这对您不起作用,您可以通过参考Madhan建议的帖子来创建类似的方法。

public WebElement findElementByText(String text){
    return driver.findElement(By.xpath("//*[contains(text(),'"+text+"')]"));
}

public WebElement findElementByText(String text, boolean exactMatch){
    if (exactMatch){
        return driver.findElement(By.xpath("//*[text()='" + text + "']"));
    }else{
        return findElementByText(text);
    }
}