Selenium Web Driver: findElement(By.name .....和无头浏览器

3
我正在尝试跟随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”元素在代码中(请在以下片段代码中查找name =“q”...)

<input spellcheck="false" dir="ltr" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; background: transparent url(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) repeat scroll 0% 0%; position: absolute; z-index: 6; left: 0px; outline: medium none;" aria-autocomplete="both" role="combobox" aria-haspopup="false" class="gsfi" id="lst-ib" maxlength="2048" name="q" autocomplete="off" title="Cerca" value="" aria-label="Cerca" type="text">

我正在使用Windows 7上的Eclipse Luna。有什么建议吗?提前感谢... Cesare

您需要使用启用了Javascript的Firefox或Chrome功能与HtmlUnitDriver。 - Vikas Ojha
嗯......我已经尝试将 "unitDriver.setJavascriptEnabled(true);" 添加到我的代码中,但仍然不起作用.... - Cesare
上面的代码对我来说运行良好。只是将“gbqfba”更改为“btnG”。 - Abhishek_Mishra
4个回答

2

我已经解决了...我在组织机构的代理后面,所以我必须设置代理。

我找到了这个:HtmlUnitDriver does not appear to be loading page

寻找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);

这里需要更新你的代理配置。


1
使用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();

我已经尝试过了,但仍然无法工作,打印的“页面标题是-->”仍为空。 - Cesare
它正在工作,我得到的是‘页面标题是:’Google。尝试在不同的浏览器中运行。 - Saritha G
我已经通过采用不同的xpath进行了编辑。请检查一下。 - Saritha G
哎呀,我正在使用Firefox 38.0.5……你是指不同的浏览器还是不同的Firefox版本?你正在使用哪个浏览器和版本? - Cesare
没有问题。我的结果完美无缺。当页面加载时,标题显示为“Google”,而当您搜索Selenium时,标题为“Selenium-Google搜索”。 - Saritha G

1

我这边运行正常,页面标题显示为“Google”。但是在执行“找到搜索按钮”的代码时出现了错误。

Unable to locate element with name: gbqfba

错误似乎出现在您的URL上,我猜测驱动程序没有将URL输入到地址栏中,因此无法导航到www.google.com网页。这就是驱动程序无法打印页面标题并查找名称为“q”的搜索编辑框的原因。
通常,这种情况发生在与浏览器和selenium jar文件相关的兼容性问题上。更新jar文件或降级浏览器可能会解决此问题。

我正在使用 Firefox 38.0.5 .... 你在使用哪个浏览器和版本?无论如何,问题似乎与浏览器版本有关(请参见Saritha上面的评论...)。我需要进行一些测试.... - Cesare
1
太棒了!这意味着我们已经向解决问题迈出了一步。下面的代码可能会解决代理问题。 driver.setProxy("xxx.xxx.xxx.xxx", port); driver.setJavascriptEnabled(true); - Manu

0

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


我已经尝试过了,但仍然无法工作,打印的“页面标题是-->”仍为空。 - Cesare

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