检查文本是否存在?

时间:2014-10-13 11:46:20

标签: xpath selenium-webdriver

说我在StackOverFlow“所有问题”页面。

  1. 我想在第一页上搜索用户说“user2402616”(出现在第3页)。
  2. 如果不存在,则单击下一步按钮,直到找到它。
  3. 我尝试使用Xpath下面的代码,但收到错误。

        boolean bPres = driver.findElement(By.xpath("//a[contains(text(),'user2402616')]")).isDisplayed();
        System.out.println(bPres);
    
        while (!bPres) {
            driver.findElement(By.xpath("//a[contains(text(),'next')]")).click();
        } 
    

1 个答案:

答案 0 :(得分:0)

由于xpath在两种情况下都有文本,即点击用户和下一个按钮。您可以使用By.linkText方法并传入要单击的值

 boolean bPres = driver.findElement(By.linkText("user2402616")).isDisplayed();
 System.out.println(bPres);

 while (!bPres) {
     driver.findElement(By.xpath("next").click();
 }