webdriver org.openqa.selenium.NoSuchElementException:无法找到元素:

时间:2015-01-25 18:22:49

标签: java selenium webdriver

我要做的是检查每个页面上的元素,如果可见,如果它在当前页面上可见,我想做出一些断言。 我的代码如下所示:

package com.example.tests;

import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;

public class Webdriver_class {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.lotto.pl/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testUntitled() throws Exception {
    driver.get(baseUrl + "/lotto/wyniki-i-wygrane/wygrane");
    assertEquals("Wyniki i wygrane Lotto i Lotto Plus | Lotto, Kaskada, Multi Multi, Mini Lotto, Joker, Zdrapki - lotto.pl", driver.getTitle());
    assertEquals("28-07-11", driver.findElement(By.xpath("//div[@id='page']/div[3]/div[2]/div[2]/table/tbody/tr[77]/td[5]")).getText());

    ///number of pages///
    String xpath = "html/body/div[3]/div[1]/div/div[3]/div[2]/div[2]/div[3]/div/ul/li";
    List<WebElement> elements = driver.findElements(By.xpath(xpath));
    int x=elements.size();
    System.out.println("liczba stron = "+elements.size());

    ////end//////


   for(int i=1; i<=x; i++){ 


            if(driver.findElement(By.xpath("//div[@id='page']/div[3]/div[2]/div[2]/table/tbody/tr[contains(td[3],'Warszawa') and contains (td[5],'21-02-09')]/td[1]")) != null)

              { assertEquals("100.", driver.findElement(By.xpath("//div[@id='page']/div[3]/div[2]/div[2]/table/tbody/tr[contains(td[3],'Warszawa') and contains (td[5],'21-02-09')]/td[1]")).getText());
               assertEquals("100.", driver.findElement(By.xpath("//div[@id='page']/div[3]/div[2]/div[2]/table/tbody/tr[contains(td[3],'Warszawa') and contains (td[5],'21-02-09')]/td[1]")).getText());
             };

            if(driver.findElement(By.xpath("//div[@id='page']/div[3]/div[2]/div[2]/table/tbody/tr[contains(td[3],'Nowa Sól') and contains (td[5],'05-04-12')]/td[1]")) != null)
            { assertEquals("99.", driver.findElement(By.xpath("//div[@id='page']/div[3]/div[2]/div[2]/table/tbody/tr[contains(td[3],'Nowa Sól') and contains (td[5],'05-04-12')]/td[1]")).getText());
            };


           //go to the new page//

            driver.findElement(By.xpath("html/body/div[3]/div[1]/div/div[3]/div[2]/div[2]/div[3]/div/ul/li/a["+i+"]")).click();
                      for (int second = 0;; second++) {
                if (second >= 60) fail("timeout- nie znalazł 'Wyniki i wygrane Lotto i Lotto Plus' ");
                try { if ("Wyniki i wygrane Lotto i Lotto Plus".equals(driver.findElement(By.cssSelector("h1.title")).getText())) break; } catch (Exception e) {}
                Thread.sleep(50000);
          }

   }

    }
}

所有元素都在第一页上可见,所以第一页就可以了,但是当它转到第二页时我会收到错误:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//div[@id='page']/div[3]/div[2]/div[2]/table/tbody/tr[contains(td[3],'Warszawa') and contains (td[5],'21-02-09')]/td[1]"}
Command duration or timeout: 30.10 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

任何人都可以帮忙吗。我使用IF语句时为什么会出现此错误。 &#39;的webdriver&#39;找不到元素=不做断言,是吗? 因此,当它找不到元素时,应该更进一步

请帮助我如何使其发挥作用:)

感谢

1 个答案:

答案 0 :(得分:1)

此错误的可能原因可能是您尝试使用xpath找到的元素: -

//div[@id='page']/div[3]/div[2]/div[2]/table/tbody/tr[contains(td[3],'Warszawa') and contains (td[5],'21-02-09')]/td[1]

在第二页中不存在。因此,即使您使用if检查元素是否存在,如果webdriver无法找到该元素,那么selenium将抛出NoSuchElementException异常。为了简化,让我们用这种方式: -

if (driver.findElement(By.xpath(element_xpath)) != null)
{
  do_some_stuff
}

现在让我们按如下方式打破它: -

is_present = driver.findElement(By.xpath(element_xpath))
if(is_present != null)
{
  do_some_stuff
}

上面的代码是上一段代码的简化版本,这是您在运行程序时评估之前代码的方式。也就是说,首先评估if块内的条件(在您的情况下为driver.findElement(By.xpath()),然后将结果与null进行比较。 但在第一种情况下,当webdriver尝试使用给定的xpath定位元素时,无法找到它。所以在这一点上,webdriver会抛出NoSuchElementException异常。因此,一旦遇到异常,python就不再对它进行评估,程序也会终止。

要解决此问题,请将其放在try-catch块内: -

 try
   {
    if (driver.findElement(By.xpath(element_xpath)) != null)
      {
       do_some_stuff
      }
   }
 catch(Exception e)
    {
     System.out.println(e);
     System.out.println("Element not found");
    }  

因此,即使webdriver发现任何Exception未能找到给定元素,也不会终止程序,它将进一步移动,下一段代码将被执行,因为catch块将处理exception并让您的计划进一步发展。

我建议您将每个if块放在try-catch内,这样,即使您的if块中有任何一个exception,下一个if } block不受exception的影响。

希望,这可以解决您的问题。

相关问题