java Selenium Web 驱动程序:findElement(By.name ..... 和无头浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30799545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium Web Driver: findElement(By.name ..... and headless browser
提问by Cesare
I'm trying to follow the Selenium Webdrive Tutorial
我正在尝试遵循 Selenium Webdrive 教程
http://www.toolsqa.com/selenium-webdriver/headless-browser-testing-selenium-webdriver/
http://www.toolsqa.com/selenium-webdriver/headless-browser-testing-selenium-webdriver/
There is a simple test, here you are the steps:
有一个简单的测试,下面是步骤:
Open webpage http://google.com
Get the title of the page.
Search for ‘Selenium'
Check the title of the page again.
获取页面的标题。
搜索“硒”
再次检查页面标题。
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
No page name is printed: ????? It seems that the "q" element in the page is not found. ????
没有打印页面名称:????? 似乎没有找到页面中的“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 ...)
我已经检查过 Firebug 并且似乎代码中存在“q”元素(在以下代码段中查找 name="q" ...)
<input spellcheck="false" dir="ltr" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; background: transparent url("data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D") 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">
I'm using Eclipse Luna on Windows 7
我在 Windows 7 上使用 Eclipse Luna
Any suggestions? Thank you in advance ...
有什么建议?先感谢您 ...
Cesare
切萨雷
回答by Cesare
I've solved .... I'm behind a proxy in my organization so I've to set Proxy.
我已经解决了......我在我的组织中支持代理,所以我必须设置代理。
I've found this: HtmlUnitDriver does not appear to be loading page.
我发现了这一点:HtmlUnitDriver 似乎没有加载页面。
Look for FunThomas424242 comment and watch this link https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.html
查找 FunThomas424242 评论并观看此链接https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.html
So the right code is the follow:
所以正确的代码如下:
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());
}
}
The "core" rows are the following
“核心”行如下
// Necessary set Proxy if you're behind it !!!!
unitDriver.setProxy("proxy.YOUR-ORGANIZATION.COM", XXXX);
where you've to update with your proxy configuration.
您必须使用代理配置更新的位置。
回答by Saritha G
Use xpath instead of name.
使用 xpath 而不是名称。
try to use this code:
尝试使用此代码:
WebElement searchBox = unitDriver.findElement(By.xpath("//input[@name='q']"));
For search button click:
对于搜索按钮,请单击:
// find the search button
WebElement button = unitDriver.findElement(By.xpath("//input[@value='Google Search']"));
// Click the button
button.click();
回答by Manu
It is working fine at my end and printing the title of page as 'Google'. Though it gave me error at 'find the search button' code.
它在我结束时工作正常并将页面标题打印为“Google”。尽管它在“查找搜索按钮”代码中给了我错误。
Unable to locate element with name: gbqfba
The error seems to be somewhere with your URL as what I can guess is that the driver is not taking the URL into address bar and, hence, not navigating to www.google.com webpage. That's the reason the driver is unable to print the page title and find the search edit box with name 'q'.
错误似乎与您的网址有关,因为我可以猜测驱动程序没有将网址放入地址栏中,因此没有导航到 www.google.com 网页。这就是驱动程序无法打印页面标题并找到名称为“q”的搜索编辑框的原因。
This generally happens due to compatibility issue related to browsers and selenium jar file. Updating the jar files or downgrading the browser may solve this issue.
这通常是由于与浏览器和 selenium jar 文件相关的兼容性问题而发生的。更新 jar 文件或降级浏览器可能会解决此问题。
回答by Gunjan Gupta
you can try using using xpath with //*[@id='sb_ifc0']
您可以尝试使用 xpath 与 //*[@id='sb_ifc0']