Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver - how to verify multiple lines text

I have an html page with following contents:

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

How can I verify all the static text above using one webdriver command?

I tried these but none of it 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')]"))

Anyone know how to do it?

like image 388
qa_enq Avatar asked May 22 '12 07:05

qa_enq


People also ask

How do you validate paragraphs in Selenium?

You can do: webDriver. findElement(By.id("<ID of center tag>")). getText().


2 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 a locator of your choice instead of By.id.

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

like image 174
Ashwin Prabhu Avatar answered Oct 12 '22 01:10

Ashwin Prabhu


This is more a limitation of XPath than Selenium.

I do not see why you are using the global XPath selector there, a better XPath selector would be:

By.XPath("//center");

If it's the only "center" tag on the page, or even:

By.XPath("//center[contains(text(), 'This is a Login page'")

The /r and /n characters will be retained if you do it through code instead of blindly searching for it in XPath.

like image 40
Arran Avatar answered Oct 11 '22 23:10

Arran