2011-01-05 107 views
4

我试图运行此程序:如何解决org.openqa.selenium.NoSuchElementException

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”组织.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.HtmlUn itDriver.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) 在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)

请他让我解决它。

+1

我建议使用Selenium IDE。这样,您可以通过所需页面“驱动”,然后将代码格式化为所需的语言。 – bakoyaro 2011-01-05 06:16:59

+0

找不到这个问题的答案吗?我从昨天开始搜索,但没有人为此提供解决方案。我想知道有没有解决这个问题的解决方案? – vkrams 2012-06-17 10:28:29

+0

HtmlUnitDriver是在执行JS坏。如果目标网页使用了大量的OS JS甚至AJAX HtmlUnitDriver它是没有选择。关于硒卷筒纸驱动器的最大好处是,你可以运行与真正的浏览器相同的代码,在其他的答案中提到... – alfonx 2013-08-17 22:23:22

回答

5

该页面上没有name =“q”的元素,因此NoSuchElementException。你从谷歌的例子,并改变它的网站,但它仍然在寻找一个谷歌搜索框在页面上。

1

使用Selenium站点中的示例,完全如此,测试将以相同的NoSuchElementException失败。使用浏览器仿真例如BrowserVersion.FIREFOX_3进行实例化时,它也会失败。

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

更新:我坐在代理的后面,不像使用真正的浏览器的驱动程序,这个驱动程序不知道代理。它必须在测试情况下进行手工配置与呼叫:

HtmlUnitDriver.setProxy(host, port); 

我还没有想出如何与用户名和密码,将其配置为那些需要身份验证的代理。

1

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

WebDriver driver = new FirefoxDriver(); 
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,它的工作!

0

我们不能用普通WebElement对于提交

相反,你可以尝试

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