如何解决org.openqa.selenium.NoSuchElementException

时间:2011-01-05 06:11:21

标签: java selenium

我正在尝试运行此程序:

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

我得到以下例外:

  

线程“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       在org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:651)       在org.openqa.selenium.By $ 4.findElement(By.java:148)       在org.openqa.selenium.htmlunit.HtmlUnitDriver $ 4.call(HtmlUnitDriver.java:1133)       在org.openqa.selenium.htmlunit.HtmlUnitDriver $ 4.call(HtmlUnitDriver.java:1)       在org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:869)       在org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1130)       在org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:330)       在com.webdrivertest.HtmlDriver.main(HtmlDriver.java:20)

请帮我解决。

5 个答案:

答案 0 :(得分:5)

答案 1 :(得分: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时,它是可行的!

答案 2 :(得分:1)

使用Selenium站点中的示例,完全不同,测试失败并使用相同的NoSuchElementException。当使用BrowserVersion.FIREFOX_3等浏览器仿真实例化时,它也会失败。

HtmlUnitDriver是否可以工作? 是否需要先以某种方式配置它?

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

HtmlUnitDriver.setProxy(host, port);

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

答案 3 :(得分:1)

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

WebDriver driver = new FirefoxDriver();

答案 4 :(得分:0)

我们无法使用普通WebElement提交

相反,你可以尝试

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