0

I am unable to locate an element in selenium, which i used htmlUnitDriver. well driver is working fine but I'm unable to find google search text box element.

Here is the code:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class SampleUnitDriver 
{
    public static void main(String[] args) throws Exception 
    {

            HtmlUnitDriver unitDriver = new HtmlUnitDriver();
            unitDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            unitDriver.get("http://google.com");
            System.out.println("Title of the page is -> " + unitDriver.getTitle());

            WebElement searchBox = unitDriver.findElement(By.xpath(".//*[@id='gs_htif0']"));
            searchBox.sendKeys("Selenium");
            WebElement button = unitDriver.findElement(By.name("gbqfba"));
            button.click();
            System.out.println("Title of the page is -> " + unitDriver.getTitle());


    }
}

Here is an Error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using .//*[@id='gs_htif0'] For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46' System info: host: 'user-PC', ip: '192.168.1.52', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51' Driver info: driver.version: SampleUnitDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:1165) at org.openqa.selenium.By$ByXPath.findElement(By.java:361) at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1725) at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1721) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1367) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1721) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:606) at com.digitalmqc.automation.action.SampleUnitDriver.main(SampleUnitDriver.java:19)

Any help can be appreciated.

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
sandeep kumar
  • 195
  • 1
  • 4
  • 20

3 Answers3

1

You are locating wrong element, You should try as below :-

HtmlUnitDriver unitDriver = new HtmlUnitDriver();

unitDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
unitDriver.get("http://google.com");
System.out.println("Title of the page is -> " + unitDriver.getTitle());

WebElement searchBox = unitDriver.findElement(By.name("q"))
searchBox.sendKeys("Selenium");
WebElement button = unitDriver.findElement(By.name("btnG"));
button.click();
System.out.println("Title of the page is -> " + unitDriver.getTitle());

Hope it helps..:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
0

Add some explicit wait before finding element:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement searchBox = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='gs_htif0']"))));
searchBox.sendKeys("Selenium");
Ranjith's
  • 4,508
  • 5
  • 24
  • 40
  • Sry buddy i am getting this error : ** Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for element to be clickable: By.xpath: .//*[@id='gs_htif0'] ** – sandeep kumar Jul 15 '16 at 12:13
  • Try changing timeout to 20 seconds and also make sure that xpath is correct, if not try to use cssSelector ex: `input[type='search']` – Ranjith's Jul 15 '16 at 12:17
  • Ohh man no use with css selector and i have given 20 sec still no use :( – sandeep kumar Jul 15 '16 at 12:24
  • Try using jsexector to input value "JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("document.getElementById('IDHERE').value = 'Selenium';");" – Ranjith's Jul 15 '16 at 12:27
  • First try upgrading/downgrading selenium version then try js executor – Ranjith's Jul 15 '16 at 12:27
0

I was also facing same issue. but it is resolved after added proxy details. you can verify if you are facing same issue or not by adding following code just after get method call

 System.out.println(driver.getCurrentUrl());
    System.out.println(driver.getPageSource()); 

you will see out put with

http://www.google.com/
Unknown host

so you need to add the proxy to your code

WebDriver  driver = new HtmlUnitDriver(BrowserVersion.CHROME,true);

        Proxy proxy = new Proxy();
        proxy.setHttpProxy("XXX.XX.XX.XX:8080"); 
        ((HtmlUnitDriver) driver).setProxySettings(proxy);

Hope this will help anyone. surely it will save few hours of search