Selenium WebDriver - how to check multiple line text

I have an html page with the following content:

<center> This is Login page. <br> Please click below link to Login. <br> <a href="xxx">Login</a> </center> 

How can I check all the static texts above using a single webdriver command?

I tried these, but none of this works :(

 driver.findElement(By.xpath("//*[contains(.,'This is Login page.<br>Please click below link to Login')]")) driver.findElement(By.xpath("//*[contains(.,'This is Login page.\nPlease click below link to Login')]")) 

Does anyone know how to do this?

+6
source share
3 answers

You can do:

 webDriver.findElement(By.id("<ID of center tag>")).getText().contains("This is Login page.Please click below link to Login.Login"); 

Feel free to use the locator of your choice instead of By.id.

Basically, getText () of any WebElement returns the text content of the node, devoid of all HTML tags and comments.

+6
source

This is more of an XPath limitation than Selenium.

I do not understand why you are using the global XPath selector, the best XPath selector:

 By.XPath("//center"); 

If this is the only "central" tag on the page or even:

By.XPath ("// center [contains (text ()," This is the login page ")

The characters / r and / n will be preserved if you do this using code instead of blind searching in XPath.

+1
source

How to find texts if texts have a space between them.

  <br> <br/> Please click below link to Login. <br> <a href="xxx">Login</a> </center> 
-1
source

Source: https://habr.com/ru/post/916328/


All Articles