简体   繁体   English

Selenium Web驱动程序:findElement(By.name…和无头浏览器

[英]Selenium Web Driver: findElement(By.name … and headless browser

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: 有一个简单的测试,这里是步骤:

  1. Open webpage http://google.com 开启网页http://google.com

  2. Get the title of the page. 获取页面标题。

  3. Search for 'Selenium' 搜索“ Selenium”

  4. 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(&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">

I'm using Eclipse Luna on Windows 7 我在Windows 7上使用Eclipse Luna

Any suggestions? 有什么建议么? Thank you in advance ... 先感谢您 ...

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似乎没有正在加载page

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. 您必须在其中更新代理配置的位置。

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

It is working fine at my end and printing the title of page as 'Google'. 在我的终端运行正常,并将页面标题打印为“ Google”。 Though it gave me error at 'find the search button' code. 尽管它在“查找搜索按钮”代码时给了我错误。

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. 该错误似乎与您的URL有关,因为我可以猜测是驱动程序没有将该URL放入地址栏中,因此也没有导航到www.google.com网页。 That's the reason the driver is unable to print the page title and find the search edit box with name 'q'. 这就是驱动程序无法打印页面标题并无法找到名称为“ q”的搜索编辑框的原因。

This generally happens due to compatibility issue related to browsers and selenium jar file. 这通常是由于与浏览器和Selenium jar文件相关的兼容性问题而发生的。 Updating the jar files or downgrading the browser may solve this issue. 更新jar文件或降级浏览器可能会解决此问题。

您可以尝试将xpath与//*[@id='sb_ifc0']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium 2:代码“driver.findElement(By.name(“username”)).sendKeys(“”);”有问题 - Selenium 2: something wrong with code “driver.findElement(By.name(“username”)).sendKeys(“”);” org.openqa.selenium.firefox.FirefoxDriver.findElement(By.name(&quot;q&quot;)) 找到一个“q”元素,但它在哪里? - org.openqa.selenium.firefox.FirefoxDriver.findElement(By.name("q")) finds a "q" element but where is it? 硒2 driver.findElement() - Selenium 2 driver.findElement() Selenium Web驱动程序的FindElement()方法不起作用(NoSuchElementException) - Selenium Web Driver FindElement() method doesnt work(NoSuchElementException) 硒中的element.findElement()方法如何工作[不是driver.findElement()] - How element.findElement() method in selenium works [not driver.findElement()] 硒2:“ driver.findElement(By…)”出了点问题; - Selenium 2: something wrong with “driver.findElement(By…); 使用Java反射API调用Selenium Web驱动程序方法&#39;findElement&#39;时代码中的问题 - Issue in code while invoking selenium web driver method 'findElement' using java reflection api 如何将Tor浏览器用作Web驱动程序硒? - How to use the tor browser as web driver selenium? 使用selenium web驱动程序复制浏览器会话 - replicate browser session with selenium web driver 配置Selenium + Headless驱动程序+ Maven - Configure Selenium + headless driver + maven
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM