java - Selenium Web 驱动程序 : findElement(By. 名称 ..... 和 headless 浏览器

标签 java selenium selenium-webdriver webdriver headless-browser

我正在尝试遵循 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

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

切萨雷

最佳答案

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

我找到了这个: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);

您必须在何处更新您的代理配置。

关于java - Selenium Web 驱动程序 : findElement(By. 名称 ..... 和 headless 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30799545/

相关文章:

java - IntelliJ IDEA 无法再运行我的 Gradle 运行配置

java - 为 Selenium 测试设置 Jenkins Windows 从屏幕分辨率

python - 如何使用 selenium python 单击文本按钮

python - 当 Selenium 测试失败时停止 ChromeDriver 回溯日志

selenium - FileUtils 未显示在 selenium WebDriver 中导入屏幕截图功能的预定义类的建议

java - apiDoc 中 @apiParam 的用法

java - mvn install 依赖项忽略 destFileName :copy-dependencies

java - 运行测试时禁用 Spring Cloud Consul

TESTNG:如何将可通过电子邮件发送的报告作为电子邮件发送

java - 使用 TestNG 从 selenium 测试用例中的页面对象调用方法时如何修复 NullPointerException 错误?