Menu

#1591 Cannot locate an element by xpath in Javascript

2.4
closed
RBRi
1
2020-02-17
2014-04-01
Yang
No

I'm trying to use Nodejs + selenium-webdriver + mocha to do some web-based automation. But when I use HTMLUnit to run the test, By.xpath() always report NoSuchElement error, even though the element is really there. Same code can work well with FireFox and other browsers.

I found a same issue reported to selenium https://code.google.com/p/selenium/issues/detail?id=6419, and it's doing the same thing with what I do, but only in Java.
Here is a sample for this problem.


// Sample code provided by selenium-webdriver
// http://selenium.googlecode.com/git/docs/api/javascript/index.html

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
// withCapabilities(webdriver.Capabilities.htmlunit()).
withCapabilities(webdriver.Capabilities.firefox()).
build();

driver.get('http://www.google.com/ncr');

// Can be accessed successfully
//driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
// Element can't be found by HTMLUnit, but works fine with FF
driver.findElement(webdriver.By.xpath('//*[@id="gbqfq"]')).sendKeys('webdriver');

driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
return driver.getTitle().then(function(title) {
return title === 'webdriver - Google Search';
});
}, 1000);
driver.quit();


More information about my environment:
Nodejs : v0.10.22
selenium-webdriver module: 1.3.14
selenium-webdriver server jar: selenium-server-standalone-2.40.0.jar

1 Attachments

Discussion

  • Yang

    Yang - 2014-04-01

    Sorry not sure about the correct HTMLUnit version I'm using, since it's included in the selenium-webdriver component. I picked 2.40 because it's the same with the selenium I'm using...

     
  • RBRi

    RBRi - 2014-04-05

    Yang,

    please provide a simple testcase (http://htmlunit.sourceforge.net/submittingJSBugs.html). If you can show the problem with a simple Html page, we will try to fix this.

     
  • RBRi

    RBRi - 2014-04-07
    • status: open --> pending
     
  • RBRi

    RBRi - 2014-04-16

    This simple java code works with the latest code from svn (when simulation FF17, FF24 and IE11)

     WebDriver driver = getWebDriver();
     driver.get("http://www.google.com/ncr");
     WebElement elem = driver.findElement(By.xpath("//*[@id=\"gbqfq\"]"));
     System.out.println(elem);
    

    This is of course java code, but keep in mind that the HtmlUnit development team is small and we can not support all languages and tools working on top of HtmlUnit.
    Additionally i have added another test to our test suit, that also shows that this xpath works.

    Will close this issue. If you still have a problem, please try to extract a simple testcase.

     
  • RBRi

    RBRi - 2014-04-16
    • status: pending --> closed
    • assigned_to: RBRi
     
  • Bhagyashri Bisoyi

    am also facing the same issue with Htmlunit driver,but same code/xpath is working in Chrome driver. can somebody please help to resolve the issue.

     

Log in to post a comment.