KangarooChief
KangarooChief

Reputation: 391

Selecting meta tags with Selenium

I want to extract data from a meta tag but the tag is as follows

<meta property="og:description" content="blah"/>

Since there is no id/class/etc. I can't use

driver.FindElement(By.[id/class/etc.]);

This meta tag has a unique property and content so I am wondering if there is any better way to locate and extract the content than selecting all "meta" tags and iterating through them.

Upvotes: 3

Views: 2837

Answers (3)

iamsankalp89
iamsankalp89

Reputation: 4739

You can use this xpaths

driver.FindElement(By.XPath("//meta[contains(@property,'og:description']"));

or

driver.FindElement(By.XPath("//meta[contains(@property,'og:description') and contains(@content,'blah')]"));

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193208

While extracting data from a meta tag, I would suggest to use the attributes as much as possible. In your case :

  • XPath:

    driver.FindElement(By.XPath("//meta[@property='og:description' and @content='blah']"));
    
  • CssSelector:

    driver.FindElement(By.CssSelector("meta[property='og:description'][content='blah']"));
    

Upvotes: 2

Jamie Rees
Jamie Rees

Reputation: 8183

You can use xPath to grab the specified meta tag

driver.FindElement(By.XPath("//meta[@property='og:description']"));

Upvotes: 2

Related Questions