Want to get the content of meta description of page using webdriver.
Let say , from below DOM want to retrieve text
Test.com provides a complete software solution for creating online tests and managing enterprise and specialist certification programs, in up to 22 languages
<script src="content/js/jquery.min.js">
<meta content="Test.com provides a complete software solution for creating online tests and managing enterprise and specialist certification programs, in up to 22 languages." name="description">
<meta content="Test.com" name="keywords">
I tried with
System.out.println(driver.findElement(By.xpath("//meta[@name='description']")).getText());
But above code not worked for me.
We can extract text from a webpage using Selenium webdriver and save it as a text file using the getText method. It can extract the text for an element which is displayed (and not hidden by CSS).
Selenium WebDriver defines two methods for identifying the elements, they are findElement and findElements . findElement: This command is used to uniquely identify a web element within the web page. findElements: This command is used to uniquely identify the list of web elements within the web page.
You're trying to get an attribute value, so instead of getText()
use getAttribute()
:
driver.findElement(By.xpath("//meta[@name='description']"))
.getAttribute("content")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With