简体   繁体   中英

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.

您可以使用xPath来获取指定的meta标签

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

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']")); 

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')]"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM