【问题标题】:Selenium Web Driver: findElement(By.name ..... and headless browserSelenium Web 驱动程序:findElement(By.name ..... 和无头浏览器
【发布时间】:2015-06-12 09:19:46
【问题描述】:

我正在尝试遵循 Selenium Webdrive 教程

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

有一个简单的测试,步骤如下:

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

  2. 获取页面标题。

  3. 搜索“硒”

  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

有什么建议吗?提前谢谢你...

切萨雷

【问题讨论】:

  • 您需要使用 HtmlUnitDriver 和 Firefox 或 Chrome 功能并打开 Javascript。
  • uhmmmm .... 我尝试添加“unitDriver.setJavascriptEnabled(true);”到我的代码,但它仍然不起作用....
  • 上面的代码对我来说工作正常。刚刚将“gbqfba”改为“btnG”。

标签: java selenium selenium-webdriver webdriver headless-browser


【解决方案1】:

我已经解决了......我在我的组织中使用代理,所以我必须设置代理。

我发现了这个: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);

您必须在哪里更新您的代理配置。

【讨论】:

    【解决方案2】:

    使用 xpath 代替名称。

    尝试使用此代码:

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

    【讨论】:

    • 我试过了,但还是不行,打印出来的“页面标题是-->”还是空
    • 它的工作,我得到'页面的标题是:'谷歌。尝试在不同的浏览器中运行。
    • 我通过使用不同的 xpath 进行了编辑。一旦检查它。
    • ops .. 我使用的是 Firefox 38.0.5 .... 你的意思是不同的浏览器还是不同的 Firefox 版本?您使用的是哪个浏览器和版本?
    • 一个小新闻......当我使用“www.google.com”时出现,因此在页面中找不到任何内容,因为......根本没有页面! .... 但这很奇怪,因为在我的 Firefox 中我已经更正了我的代理配置。我可以在 Eclipse 的 Java 代码中的某个位置进行配置吗?怎么样?
    【解决方案3】:

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

    Unable to locate element with name: gbqfba
    

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

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

    【讨论】:

    • 我使用的是 Firefox 38.0.5 .... 您使用的是哪个浏览器和版本?在任何情况下,问题似乎都与浏览器版本有关(上面的 Saritha 评论...)。我必须做一些测试....
    • 一个小新闻......当我使用“www.google.com”时出现,因此在页面中找不到任何内容,因为......根本没有页面! .... 但这很奇怪,因为在我的 Firefox 中我已经更正了我的代理配置。我可以在 Eclipse 的 Java 代码中的某个位置进行配置吗?怎么样?
    • 酷!这意味着我们在解决问题方面领先一步。下面的代码可以解决代理问题。 driver.setProxy("xxx.xxx.xxx.xxx", 端口); driver.setJavascriptEnabled(true);
    【解决方案4】:

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

    【讨论】:

    • 我试过了,但还是不行,打印出来的“页面标题是-->”还是空
    猜你喜欢
    • 1970-01-01
    • 2018-04-02
    • 2019-12-14
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 2019-02-22
    • 1970-01-01
    相关资源
    最近更新 更多