Selenium Web驱动程序:findElement(By.name .....和无头浏览器 [英] Selenium Web Driver: findElement(By.name ..... and headless browser

查看:150
本文介绍了Selenium Web驱动程序:findElement(By.name .....和无头浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循Selenium Webdrive教程

I'm trying to follow the Selenium Webdrive Tutorial

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

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

There is a simple test, here you are the steps:


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

获取页面标题。

搜索'Selenium'

Search for ‘Selenium’

再次检查页面标题。

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

Starting from the class code sample, here you are my code

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

   }
}

尝试执行它我已经以下错误

Trying to execute it I've the following error

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

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

No page name is printed: ????? It seems that the "q" element in the page is not found. ????

我已经检查过Firebug并且似乎代码中有q元素(在下面的snipplet中查找name =q)代码...)

I've checked with Firebug and seems that the "q" element there is in the code (look for name="q" in the following snipplet code ...)

<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

I'm using Eclipse Luna on Windows 7

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

Any suggestions? Thank you in advance ...

Cesare

推荐答案

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

I've solved .... I'm behind a proxy in my organization so I've to set Proxy.

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

寻找FunThomas424242评论并观看此链接 https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.html

Look for FunThomas424242 comment and watch this link 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);

您需要使用代理配置进行更新。

where you've to update with your proxy configuration.

这篇关于Selenium Web驱动程序:findElement(By.name .....和无头浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆