簡體   English   中英

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

[英]webdriver org.openqa.selenium.NoSuchElementException: Unable to locate element:

我想做的是檢查每個頁面上的元素,如果可見以及在當前頁面上是否可見,我想提出一些主張。 我的代碼如下所示:

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語句時出現此錯誤。 'Webdriver'找不到元素=不聲明,是嗎? 因此,當找不到元素時,應該走得更遠

請幫我做什么使它工作:)

謝謝

該錯誤的可能原因可能是您嘗試使用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發現任何無法定位給定元素的異常,它也不會終止程序,而是會進一步移動,並執行下一部分代碼,因為catch塊將處理exception並讓您的程序移動進一步。

我建議您將每個if塊放入try-catch ,因此,即使您的任何if塊都具有exception ,下一個if塊也不會受到其exception影響。

希望這能解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM