vaibhav chitransh
vaibhav chitransh

Reputation: 11

Verify text on web page and then get the location in selenium webdriver using Java

Is their any way we can find the location of Text present on web page after verifying its presence using selenium webdriver in java

Code used to verify the text present of not:

driver.get("http://newtours.demoaut.com/");

driver.getPageSource().contains("Mercury Tours!");
if(true)
{
    System.out.println("Test Case Pass");
} 
else    
    System.out.println("Test Case Fail");   

Mercury Tours! is text on a web page. Now how to find location after getting the text = true.

I tried using getLocation(), but failed.

Please let me know the solution on this asap.

Upvotes: 1

Views: 2011

Answers (1)

nilesh
nilesh

Reputation: 14287

Because of the complexity of DOM structure and lack of unique attributes on the elements, I think you should just use xpath to find an element with text. Unfortunately this is the only option I see as of now unless others have better suggestions. Ask front end developers to provide unique attributes so that you can find elements easily instead of relying on text of the element.

driver.get("http://newtours.demoaut.com/");
WebElement element = driver.findElement(By.xpath(".//font[contains(text(),'Mercury Tours')]"));
if(element.isDisplayed()) {
   System.out.println("Test Case Pass");
} else    
    System.out.println("Test Case Fail");   
}

Upvotes: 1

Related Questions