NoSuchElementException while using Xpath with HtmlUnitDriver

1.2k views Asked by At

I need to run Selenium in Linux machine and using HtmlUnitDriver to achieve this.

Selenium script contains most of Xpath to find the elements. While running, NoSuchElementException is getting displayed at the places where xpath is used.

org.openqa.selenium.NoSuchElementException: Unable to locate a node using //*[contains(text(),'Sample Text')]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

The same code is working fine with other drivers like Chrome, Firefox or IE.

My code looks as below,

 HtmlUnitDriverdriver = new HtmlUnitDriver();
    driver.setJavascriptEnabled(true);
    driver.get("http://example.com");
    driver.findElement(By.id("username")).sendKeys("xxx");
    driver.findElement(By.id("password")).sendKeys("yyy");
    driver.findElement(By.id("loginButton")).click();
    Thread.sleep(2000);
    driver.findElement(By.xpath("//*[contains(text(),'Sample Text')]")).click();

I found many similar question and the solution is to provide sleep time or to wait for the element till it gets loaded. I provided everything and still facing this issue when xpath is used.

Also finding the element by ID or name is not possible in my use case as it varies everytime.

I can use only the xpath to find the text inside the span tag.

I need a solution for handling this. Thanks in advance!

Edit :

I tried the same for gmail login, Still am getting the same issue,

driver.get("http://www.gmail.com");
        driver.findElement(By.id("Email")).sendKeys("[email protected]");
        driver.findElement(By.id("next")).click();
        Thread.sleep(2000);
        driver.findElement(By.id("Passwd")).sendKeys("yyy");
        driver.findElement(By.id("signIn")).click();
        Thread.sleep(8000);
        driver.findElement(By.xpath("//*[contains(text(),'COMPOSE')]")).click();
2

There are 2 answers

0
Florent Georges On

Consider also getting rid of text(), which might cause that kind of problem. Use this instead:

contains(., 'Sample Text')

Not sure this is the cause if your problem here, this is more of a blind shot, given the few info you gave us.

0
Kudin On

I would first of all suggest providing a more effective explicit wait, so that it will suspend until the condition is fulfilled, and also will give you the TimeoutException if you didn't provide long enough timeout.

//This will store your locator for later use
By elementLocator = By.xpath("//*[contains(text(),'Sample Text')]");

//This creates a reusable wait object
WebDriverWait wait = new WebDriverWait(browser.driver, 15);

//Waiting is stopped right after the condition is satisfied or timeout (set in the previous line, in secs) was reached
wait.until(ExpectedConditions.elementToBeClickable(elementLocator));

If you are getting the same error you can try this

try {
    wait.until(ExpectedConditions.elementToBeClickable(elementLocator));
} catch (org.openqa.selenium.TimeoutException e) {
    System.out.println(driver.getPageSource());
}

To see whether the element is present on the page you are looking for it.