java 如何解决 org.openqa.selenium.NoSuchElementException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4601296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:09:48  来源:igfitidea点击:

How to solve the org.openqa.selenium.NoSuchElementException

javaselenium

提问by d5111

I am trying to run this program:

我正在尝试运行这个程序:

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class HtmlDriver {
  public static void main(String[] args) {
     // Create a new instance of the html unit driver
     // Notice that the remainder of the code relies on the interface,
     // not the implementation.
  WebDriver driver = new HtmlUnitDriver();

      // And now use this to visit Google
      driver.get("http://www.stumbleupon.com/home/");

      // Find the text input element by its name
     WebElement element = driver.findElement(By.name("q"));

      // Enter something to search for
      element.sendKeys("Cheese!");

      // Now submit the form. WebDriver will find the form for us from the element
      element.submit();

      // Check the title of the page
      System.out.println("Page title is: " + driver.getPageSource());
 }
}

And I am getting the following exception:

我收到以下异常:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q System info: os.name: 'Linux', os.arch: 'i386', os.version: '2.6.27-7-generic', java.version: '1.6.0_12' Driver info: driver.version: HtmlDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:651) at org.openqa.selenium.By$4.findElement(By.java:148) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1133) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:869) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1130) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:330) at com.webdrivertest.HtmlDriver.main(HtmlDriver.java:20)

线程“main” org.openqa.selenium.NoSuchElementException 中的异常:无法定位元素名称:q 系统信息:os.name:'Linux',os.arch:'i386',os.version:'2.6.27- 7-generic', java.version: '1.6.0_12' 驱动程序信息:driver.version: HtmlDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:651) at org.openqa.selenium.By$4 .findElement(By.java:148) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1133) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:869) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1130) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:330) 在 com.webdrivertest.HtmlDriver.main(HtmlDriver.java:20)

Please help me out in resolving it.

请帮我解决它。

回答by Nick

There's no element with name="q" on that page, hence, NoSuchElementException. You took the example from google and changed the site it goes to, but it's still looking for a google search box on the page.

该页面上没有 name="q" 的元素,因此,NoSuchElementException。您从 google 获取了示例并更改了它访问的站点,但它仍在页面上寻找 google 搜索框。

回答by erhun

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Example  {
    public static void main(String[] args) {
        // Create a new instance of the html unit driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        try {
            WebDriver driver = new FirefoxDriver();

            // And now use this to visit Google
            driver.get("http://www.google.com");

            // Find the text input element by its name
            WebElement element = driver.findElement(By.name("q"));

            // Enter something to search for
            element.sendKeys("Cheese!");

            // Now submit the form. WebDriver will find the form for us from the element
            element.submit();

            // Check the title of the page
            System.out.println("Page title is: " + driver.getTitle());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

When change the HtmlUnitDriver to FirefoxDriver, it is work!

当将 HtmlUnitDriver 更改为 FirefoxDriver 时,就可以了!

回答by Alan Escreet

Using the example from the Selenium site, exactly as it is, the test fails with the same NoSuchElementException. It fails also when instantiated with browser emulation such as BrowserVersion.FIREFOX_3.

使用来自 Selenium 站点的示例,完全一样,测试失败并显示相同的NoSuchElementException. 使用浏览器模拟(例如BrowserVersion.FIREFOX_3.

Does the HtmlUnitDriver work at all? Is there a need to configure it in some way first?

HtmlUnitDriver 是否有效?是否需要先以某种方式配置它?

Update:I'm sitting behind a proxy and unlike the drivers that use real browsers, this driver doesn't know about the proxy. It must be manually configured in the test case with a call to:

更新:我坐在一个代理后面,与使用真实浏览器的驱动程序不同,这个驱动程序不知道代理。它必须在测试用例中手动配置,调用:

HtmlUnitDriver.setProxy(host, port);

I haven't yet figured out how to configure it with username and password for those proxies that require authentication.

我还没有弄清楚如何为那些需要身份验证的代理配置用户名和密码。

回答by Lutz

Try defining the WebDriverto use Firefox explicitly:

尝试明确定义WebDriver使用 Firefox:

WebDriver driver = new FirefoxDriver();

回答by user3487861

We cannot use Normal WebElementFor Submit

我们不能使用 Normal WebElementFor Submit

Instead you can try

相反,你可以尝试

WebElement form = driver.findElement(By.id("formid")); //Id of FORM Tag
form.submit();