Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the content of meta description from web-page using selenium webdriver

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.

like image 213
user3302083 Avatar asked Sep 22 '14 12:09

user3302083


People also ask

How do you extract text from a Web page using selenium and save it as a text file?

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).

How do you find an element on a page in selenium?

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.


1 Answers

You're trying to get an attribute value, so instead of getText() use getAttribute() :

driver.findElement(By.xpath("//meta[@name='description']"))
      .getAttribute("content")
like image 165
har07 Avatar answered Oct 23 '22 08:10

har07