Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting meta tags with Selenium

Tags:

html

c#

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.

like image 293
KangarooChief Avatar asked Nov 26 '25 09:11

KangarooChief


2 Answers

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']"));
    
like image 132
undetected Selenium Avatar answered Nov 27 '25 21:11

undetected Selenium


You can use xPath to grab the specified meta tag

driver.FindElement(By.XPath("//meta[@property='og:description']"));
like image 32
Jamie Rees Avatar answered Nov 27 '25 23:11

Jamie Rees



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!