0

I have one 2 min script for Selenium webdriver. It works very fine with FirefoxDriver and ChromeDriver. It never gets fails with real browser.

But when I run with HTMLUnit driver I have found Its fails randomly. Throws exception like not able to find element.

Solutions that I have tried out :

  1. Add Thread.sleep to 6000 ms. But still not run every-time
  2. Add wait but it throws error at same line
  3. Change locator of element from Id to xpath or CSS path.

Configuration and Versions:

selenium-java : 2.53.0

selenium-server : 2.53.0

htmlunit-driver : 2.21

TestNG : 6.8.8

Christian Neverdal
  • 5,655
  • 6
  • 38
  • 93
Sagar007
  • 848
  • 1
  • 13
  • 33
  • HtmlUnitDriver has a very different implementation (it doesn't really use another browser). So, it is more limited and will generally fail more often. – Christian Neverdal Jun 07 '16 at 08:40

1 Answers1

1

Add explicit wait for the elements that WebDriver throws exception of unable to find element, as given below -

WebDriverWait wait = new WeBDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));

Also, ensure that you have added below dependencies to your project -

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.21</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>2.53.0</version>
</dependency>
  • I have add 30 Second in wait but still found this error. Project has updated libraries. I have also included both dependencies. – Sagar007 Jun 07 '16 at 11:36
  • Can you give more details on selenium-support ? – Sagar007 Jun 07 '16 at 11:39
  • Selenium-support is required as per official page of HtmlUnitDriver - https://github.com/SeleniumHQ/htmlunit-driver – Punkaaj Chavaan Jun 07 '16 at 14:45
  • It would be great if you could share your script and relevant necessary details so that it could be tried to reproduce. – Punkaaj Chavaan Jun 07 '16 at 14:46
  • I can not because some policies. But I can surely tell I have tried above solutions but still not able to get perfect results. – Sagar007 Jun 08 '16 at 09:03
  • try enabling the javascript - driver.setJavascriptEnabled(true); other than this you can also try to enable debug logging HtmlUnitDriver and see if you get any clue from the logs. – Punkaaj Chavaan Jun 10 '16 at 08:13