Selenium Web Driver: findElement(By.name ..... and headless browser

22,778

Solution 1

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.

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

Solution 2

Use xpath instead of name.

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

Solution 3

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.

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'.

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.

Share:
22,778
Cesare
Author by

Cesare

Updated on June 13, 2020

Comments

  • Cesare
    Cesare almost 4 years

    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. Open webpage http://google.com

    2. Get the title of the page.

    3. Search for ‘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. ????

    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">
    

    I'm using Eclipse Luna on Windows 7

    Any suggestions? Thank you in advance ...

    Cesare

  • Cesare
    Cesare almost 9 years
    I've tried it but still doesn't work and still the printed "Title of the page is -->" is null
  • Cesare
    Cesare almost 9 years
    I've tried it but still doesn't work and still the printed "Title of the page is -->" is null
  • Saritha G
    Saritha G almost 9 years
    Its working and i m getting 'Title of the page is:' Google. Try to run in different browser.
  • Saritha G
    Saritha G almost 9 years
    I have edited by taking different xpath. Once check it.
  • Cesare
    Cesare almost 9 years
    ops .. I'm using Firefox 38.0.5 .... Do you mean a different browser or a different Firefox version? Which browser and version are you using?
  • Cesare
    Cesare almost 9 years
    I'm using Firefox 38.0.5 .... Which browser and version are you using? In any case it seems that the problem is about something related to the browser version (vew Saritha comment above ...). I've to do some tests ....
  • Cesare
    Cesare almost 9 years
    A little news .... I think that is something related with proxy, because if i put a web address internal in my organization it found the page Title .... this explains because the title not appears when I use "www.google.com" and so nothing can be found in the page because ... there is no page at all! .... But this is quite strange because in my Firefox I've corrected set my proxy configuration. May I configure also somewhere in my Java code in Eclipse? How?
  • Cesare
    Cesare almost 9 years
    A little news .... I think that is something related with proxy, because if i put a web address internal in my organization it found the page Title .... this explains because the title not appears when I use "www.google.com" and so nothing can be found in the page because ... there is no page at all! .... But this is quite strange because in my Firefox I've corrected set my proxy configuration. May I configure also somewhere in my Java code in Eclipse? How?
  • Saritha G
    Saritha G almost 9 years
    No. I got perfect result. Its displaying, when page is loaded the title is:Google, And when you search for selenium,then the Title is: Selenium-Google search.
  • Manu
    Manu almost 9 years
    cool! that means we are one step ahead to solve the problem. The code below may solve the proxy issue. driver.setProxy("xxx.xxx.xxx.xxx", port); driver.setJavascriptEnabled(true);