Search code examples
pythonseleniumxpathfacebook-opengraphmeta-tags

Python Selenium Issue With Meta Open Graph xpath selector


I have confirmed the xpath works using a Google Chrome plugin that tests xpaths. This is the xpath:

//meta[@property='og:url']/@content

and this is the line of code that works with other xpaths so I know the only variable is this current xpath:

pageID = get_data(driver,"//meta[@property='og:url']/@content")

But when I run my Python Selenium script I get the error:

"invalid selector" "it should be an element."

Am I only allowed to use xpaths that are visible? How can I select hidden elements that require view-page source?

NOTE: Thank you and my apologies if I am missing any information. This is my first post here and I only created a profile because I searched everywhere online and couldn't find a solution.


Solution

  • It is not clear from the question how the method get_data() is defined.

    However, to extract the pageID you can use find_element_by_xpath() along with get_attribute() method as follows:

    pageID = driver.find_element_by_xpath("//meta[@property='og:url']").get_attribute("content")