6

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?

qa_enq
  • 61
  • 1
  • 1
  • 3

4 Answers4

6

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.

Ashwin Prabhu
  • 9,285
  • 5
  • 49
  • 82
1

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.

Arran
  • 24,648
  • 6
  • 68
  • 78
  • I use the global XPath selector as it's actually codes in my common library to check text present. Perhaps I should have another method in my common library to check the text based on specific tag. But back to the question, meaning I should do smth like this instead: – qa_enq May 22 '12 at 09:15
  • Yes, I would go with the trying to find it without the /r and /n (new line and carriage returns) characters included, go for something more simple (like what I've done above) and check the text after you've got the element. It will be much easier to check the structure of it in code afterwards. Failing this, you can add an ID to the center tag and use Ashwin's answer. – Arran May 22 '12 at 09:20
-1

How to find the texts if the texts has space inbetween them.

      <br> 
        <br/>
      Please click below link to Login.
      <br> 
      <a href="xxx">Login</a> 
</center>
Bibek
  • 64
  • 4
  • Found the solution on another page. If anyone is having same issue as me. use this link: https://groups.google.com/forum/#!topic/selenium-users/brPeac2QTvs and the Solution is: Assert.assertEquals("The email is blank.\nPlease enter an email!",driver.findElement(By.xpath(XXXXXXXX)).gettext()); – Bibek Nov 26 '13 at 19:39
-1

You can do with "\n"

return getText(supportText).contains("Information:" + "\n" +
    "This page is for use by the TECHNICAL TEAM only. Following links will be active, only if you have access (subscription) to those modules.");
Michael Lihs
  • 7,460
  • 17
  • 52
  • 85