Home > Software design >  How to get the text of element using Selenium Java
How to get the text of element using Selenium Java

Time:08-30

I want to get the text "We didn't recognize that email and/or password." using Selenium and Java

<div >
  <span >
      <svg  viewBox="0 0 32 32" aria-labelledby="title1" role="img" fill-rule="evenodd">
        <title id="title1">IconHelp</title>
        <path fill="inherit" ></path>
      </svg>
   </span>
  <p  data-qa-id="error-display">We didn't recognize that email and/or password.<a >Need help?</a></p>
</div>

CodePudding user response:

Try the following xpaths to trace the element -

//p[contains(text(),'We didn't recognize that email and/or password')]

Or

//p[@class='uni-text'][@data-qa-id='error-display']

Also, please share the HTML markup in properly formatted manner.

CodePudding user response:

To extract the text We didn't recognize that email and/or password. from the <p> element you can use either of the following locator strategies:

  • Using cssSelector:

    System.out.println(wd.findElement(By.cssSelector("p.uni-text[data-qa-id='error-display']")).getText());
    
  • Using xpath:

    System.out.println(wd.findElement(By.xpath("//p[@class='uni-text' and @data-qa-id='error-display']")).getText());
    
  • Related