如何解决org.openqa.selenium.NoSuchElementException问题

4

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

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

我遇到了以下异常:

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)

请帮我解决这个问题。


1
我建议使用Selenium IDE。这样你就可以“驱动”你想要的页面,然后将代码格式化为你需要的语言。 - bakoyaro
有人找到这个问题的答案吗?我从昨天开始一直在谷歌上搜索,但没有人提供解决方案。我想知道是否有任何可用的解决方案来解决这个问题? - vkrams
HtmlUnitDriver在执行JS方面表现不佳。如果目标网页使用了大量的JS甚至是AJAX,那么HtmlUnitDriver就不是一个好选择。Selenium Web Driver的优点在于,您可以在真实的浏览器中运行相同的代码,正如其他答案中所提到的一样... - alfonx
5个回答

5

该页面上没有名为“q”的元素,因此出现了NoSuchElementException。您使用了来自Google的示例并更改了其导航网站,但仍在寻找页面上的Google搜索框。


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

将HtmlUnitDriver更改为FirefoxDriver后,它就可以工作了!


1

尝试明确定义WebDriver以使用Firefox:

WebDriver driver = new FirefoxDriver();

1

使用 Selenium 网站上的示例,完全按照它的方式进行测试时,测试会出现相同的 NoSuchElementException 错误。即使使用浏览器仿真(例如 BrowserVersion.FIREFOX_3),测试也会失败。

HtmlUnitDriver 是否根本不起作用? 是否需要先对其进行某些配置?

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

HtmlUnitDriver.setProxy(host, port);

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


0

我们不能使用普通的 WebElement 进行提交

相反,您可以尝试

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接