Search code examples
javaselenium-webdriverphantomjshtmlunithtmlunit-driver

org.openqa.selenium.NoSuchElementException: Unable to locate a node using HtmlunitDriver/Phantomjs


I am using HTMLUnitDriver. It is unable to locate the xpaths and css selectors

WebDriver d=new HtmlUnitDriver();
WebDriverWait wait = new WebDriverWait(d, 10);
// d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
d.get("http://www.google.com");
//d.manage().window().maximize();
d.findElement(By.name("q")).sendKeys("flipkart");
d.findElement(By.name("btnG")).click();
Thread.sleep(5000);
String s1 = wait.until(
        ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='rhs_block']/ol/li/div[1]/div/div[1]/ol/li[2]/div/div[1]"))).getText();
//String s1=d.findElement(By.cssSelector("div.kno-ecr-pt.kno-fb-ctx")).
Assert.assertEquals(s1,"Flipkart");
System.out.println(s1);
d.close();
d.quit();

It works Fine with all browser but not HTMLUNITDRIVER

I read some posts saying that we need to give wait I tried with all the possible waits in selenium.

I have tried with PhantomJS, but with the same issue of Unable to locate xpath.

File file = new File("C:/jars/phantomjs-2.0.0-windows/bin/phantomjs.exe");             
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());        
WebDriver d = new PhantomJSDriver(); 

//WebDriver d=new HtmlUnitDriver();
WebDriverWait wait = new WebDriverWait(d, 10);
d.get("http://www.google.com");
//  d.manage().window().maximize();
d.findElement(By.name("q")).sendKeys("flipkart");
d.findElement(By.name("btnG")).click();
Thread.sleep(5000);
String s1 = wait.until(
        ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='rhs_block']/ol/li/div[1]/div/div[1]/ol/li[2]/div/div[1]"))).getText();
//String s1=d.findElement(By.cssSelector("div.kno-ecr-pt.kno-fb-ctx")).
Assert.assertEquals(s1,"Flipkart");
System.out.println(s1);
d.close();
d.quit(); 

Solution

  • I found it the problem was with the jar i was using the jar phantomjsdriver-1.1.0.jar i have changed the versions of jar now which is phantomjsdriver-1.2.1.jar works perfectly fine.

    Thanks for the help friends.