-1

I am stuck in to automate one of the functionality in my current project using Selenium TestNG. There is scenario where I want to get the text (The answer you entered was not correct.) from the div given in the source code. I am using element.getText() method to get the content but I am not able to get the text it carries.Any help would be appreciated.Thanks in advanced.

<div class="error">
<h2 class="visually-hidden">Error message</h2>
<div class="icon">
<svg version="1.1" height="24" viewBox="0 0 64 64">
</div>
The answer you entered was not correct.
</div>
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66

2 Answers2

1

You can get test using this...

   String text=  driver.findElement(By.xpath(".//*[@class='error']/h2/div")).getText();
 //print text
Zakaria Shahed
  • 2,589
  • 6
  • 23
  • 52
-1

You should be fine with:

// Assuming driver is instance of your WebDriver object
driver.findElement(By.cssSelector("div.error")).getText();
Kudin
  • 456
  • 4
  • 7