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();
Consider also getting rid of
text()
, which might cause that kind of problem. Use this instead:Not sure this is the cause if your problem here, this is more of a blind shot, given the few info you gave us.