0

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(); 
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Chaitanya Pujari
  • 367
  • 4
  • 16

4 Answers4

2

This is because the display size of HTMLUnit and PhantomJS, by default, will render the mobile version of a page, if it exists. You can work around this by setting the display size of the driver explicitly:

driver.manage().window().setSize(new Dimension(1920, 1200));

where 1920 would be the window width in pixels, and 1200 is the window height in pixels. Set them to whatever you think is acceptable.

aholt
  • 2,829
  • 2
  • 10
  • 13
1

HtmlUnitDriver might have problems with some websites. Another option for you might be to use PhantomJS. Here's a discussion about the differences between them.

Community
  • 1
  • 1
Jakub Loksa
  • 537
  • 1
  • 14
  • 32
  • i am using phantom js i don't no where i am committing mistakes does it recognizes xpaths ? id and name works fine but not xpath or css selectors – Chaitanya Pujari Aug 04 '15 at 09:41
1

When we use HtmlUnitDriver, we are not getting rhs block(flipkart info section which appears on right hand side). I tried to get page source(htmlunitdriver) and found that it does not contain html markup for rhs block. The same is coming when we use firefox driver.

Might be that rhs block requires some physical browser support to display.(MAY BE, JUST ASSUMPTION).

So, since it is not coming, you are getting NoSuchElementException for HtmlUnitDriver.

Vishal Jagtap
  • 896
  • 11
  • 16
  • yes i got the solution for was not on the write screen i maximized the window and tried for some other application its working fine nw – Chaitanya Pujari Aug 04 '15 at 11:59
0

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.

Chaitanya Pujari
  • 367
  • 4
  • 16