简体   繁体   English

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

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

What I'm trying to do is checking elements on each page, if visible and if it is visible on current page i would like to make some assertion. 我想做的是检查每个页面上的元素,如果可见以及在当前页面上是否可见,我想提出一些主张。 My code looks like below: 我的代码如下所示:

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);
          }

   }

    }
}

All elements are visible on the first page so on first page is ok but when it goes to the second page I get error: 所有元素都在第一页上可见,因此在第一页上可以,但是转到第二页时出现错误:

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

Can anyone help with it. 任何人都可以帮忙。 Why I get this error when I use IF statement. 为什么在使用IF语句时出现此错误。 'Webdriver' doesnt find element = doesnt make assertion, does it? 'Webdriver'找不到元素=不声明,是吗? So when it doesnt find element then should go further 因此,当找不到元素时,应该走得更远

Please, help me what to do to make it working :) 请帮我做什么使它工作:)

thanks 谢谢

The possible cause for this error might be that the element that you are trying to find with xpath :- 该错误的可能原因可能是您尝试使用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]

is not present in the second page. 在第二页中不存在。 So, even if you have used if for checking whether the element is present or not, if webdriver could not find the element, then selenium will throw NoSuchElementException exception. 因此,即使您使用过if来检查元素是否存在,如果webdriver无法找到该元素,那么selenium也会抛出NoSuchElementException异常。 To make it simpler, let's put it in this way :- 为了简单起见,让我们这样写:-

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

Now let's break it as follows :- 现在,让我们按如下方式进行分解:

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

The above piece of code is the simplified version of the previous one and this is how your previous code is evaluated while you run the program. 上面的代码是上一个代码的简化版本,这是在运行程序时如何评估以前的代码。 That is, first the condition inside if block is evaluated (in your case driver.findElement(By.xpath()) and then the result is compared with null . But in the first case, while webdriver tries to locate the element with the given xpath, it couldn't be found. So at that point itself webdriver throws NoSuchElementException exception. So, once exception is encountered, python doesn't evaluate it any more and the program terminates. 也就是说,首先评估if块内部的条件(在您的情况下为driver.findElement(By.xpath()) ,然后将结果与null进行比较。但是在第一种情况下,当webdriver尝试使用给定的元素定位元素时xpath找不到它,所以这时webdriver本身会引发NoSuchElementException异常,因此,一旦遇到异常,python将不再对其进行评估,程序将终止。

To solve it, put it inside try-catch block :- 要解决它,请将其放入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");
    }  

So, even if webdriver finds any Exception failing to locate the given element, instead of terminating the program it will move further and the next piece of your code will be executed, because the catch block will take care of the exception and let your program move further. 因此,即使webdriver发现任何无法定位给定元素的异常,它也不会终止程序,而是会进一步移动,并执行下一部分代码,因为catch块将处理exception并让您的程序移动进一步。

I would suggest you to put each if block within try-catch , so, even though any of your if block has an exception , the next if block would not be affected by it's exception . 我建议您将每个if块放入try-catch ,因此,即使您的任何if块都具有exception ,下一个if块也不会受到其exception影响。

Hope, this would solve your issue. 希望这能解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.openqa.selenium.NoSuchElementException:无法找到元素错误 - org.openqa.selenium.NoSuchElementException: Unable to locate element error org.openqa.selenium.NoSuchElementException:无法找到元素: - org.openqa.selenium.NoSuchElementException: Unable to locate element: org.openqa.selenium.NoSuchElementException:无法找到元素:-搜索字段 - org.openqa.selenium.NoSuchElementException: Unable to locate element:- Search field 无法找到元素:org.openqa.selenium.NoSuchElementException - Unable to locate element: org.openqa.selenium.NoSuchElementException org.openqa.selenium.NoSuchElementException:无法找到元素: - org.openqa.selenium.NoSuchElementException: Unable to locate element: org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:无法找到表格标签内的元素 - org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: not able to locate the element inside table tag 代码中的 Selenium Fluent Wait Implementation 仍然给出“org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:” - Selenium Fluent Wait Implementation in the code still gives “org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:” org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element error using Selenium through Java - org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element error using Selenium through Java org.openqa.selenium.NoSuchElementException:没有这样的元素:尝试使用Java通过Selenium登录时无法找到元素 - org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element while trying to login through Selenium with Java 线程“ main”中的异常org.openqa.selenium.NoSuchElementException:无此类元素:无法找到元素:{“ method”:“ id”,“ selector”:“ name”} - Exception in thread “main” org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:“id”,“selector”:“name”}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM