Selenium Web驱动程序:findElement(By.name …和无头浏览器

我正在尝试遵循Selenium Webdrive教程

http://www.toolsqa.com/selenium-webdriver/headless-browser-testing-selenium-webdriver/

有一个简单的测试,这里是你的步骤:

  1. 打开网页http://google.com

  2. 获取页面标题。

  3. 搜索’Selenium’

  4. 再次检查页面标题。

从类代码示例开始,这里是您的代码

package headlessBrowser; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class TestOne { public static void main(String[] args) { // Declaring and initialising the HtmlUnitWebDriver HtmlUnitDriver unitDriver = new HtmlUnitDriver(); // open google.com webpage unitDriver.get("http://google.com"); System.out.println("Title of the page is -> " + unitDriver.getTitle()); // find the search edit box on the google page WebElement searchBox = unitDriver.findElement(By.name("q")); // type in Selenium searchBox.sendKeys("Selenium"); // find the search button WebElement button = unitDriver.findElement(By.name("gbqfba")); // Click the button button.click(); System.out.println("Title of the page is -> " + unitDriver.getTitle()); } } 

试图执行它我有以下错误

 Title of the page is -> Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q 

没有打印页面名称:????? 似乎找不到页面中的“q”元素。 ????

我已经使用Firebug进行了检查,似乎代码中存在“q”元素(在下面的snipplet代码中查找name =“q”…)

  

我在Windows 7上使用Eclipse Luna

有什么建议么? 先感谢您 …

切萨雷

我已经解决了……我在我的组织中代理了一个代理,所以我要设置代理。

我发现了这个: HtmlUnitDriver似乎没有加载页面 。

寻找FunThomas424242的评论,注册链接https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.html

所以正确的代码如下:

 package headlessBrowser; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class TestOne { public static void main(String[] args) { // Declaring and initialising the HtmlUnitWebDriver HtmlUnitDriver unitDriver = new HtmlUnitDriver(); // Necessary set Proxy if you're behind it !!!! unitDriver.setProxy("proxy.YOUR-ORGANIZATION.COM", XXXX); // open google.com webpage unitDriver.get("http://www.google.com"); System.out.println("Title of the page is -> " + unitDriver.getTitle()); // find the search edit box on the google page WebElement searchBox = unitDriver.findElement(By.name("q")); // type in Selenium searchBox.sendKeys("Selenium"); // find the search button WebElement button = unitDriver.findElement(By.name("btnG")); // Click the button button.click(); System.out.println("Title of the page is -> " + unitDriver.getTitle()); } } 

“核心”行如下

  // Necessary set Proxy if you're behind it !!!! unitDriver.setProxy("proxy.YOUR-ORGANIZATION.COM", XXXX); 

您需要使用代理配置更新的位置。

使用xpath而不是name。

尝试使用此代码:

  WebElement searchBox = unitDriver.findElement(By.xpath("//input[@name='q']")); 

对于搜索按钮,请单击

  // find the search button WebElement button = unitDriver.findElement(By.xpath("//input[@value='Google Search']")); // Click the button button.click(); 

它在我的工作正常,并将页面标题打印为“Google”。 虽然它在“找到搜索按钮”代码时给了我错误。

 Unable to locate element with name: gbqfba 

错误似乎与您的url有关,因为我可以猜到的是,驱动程序没有将url带到地址栏中,因此无法导航到www.google.com网页。 这就是驱动程序无法打印页面标题并找到名为“q”的搜索编辑框的原因。

这通常是由于与浏览器和selenium jar文件相关的兼容性问题而发生的。 更新jar文件或降级浏览器可能会解决此问题。

你可以尝试使用xpath和//*[@id='sb_ifc0']