Pyderman
Pyderman

Reputation: 16227

How to find Xpath for a web element defined using a Javascript tooltip?

When the Tweets figure on someone's Twitter page is in the thousands, Twitter rounds it to e.g 12.4K. When you move the mouse over this element, a tooltip tells you the true number e.g. 12,412.

I'm looking to get this actual figure using Selenium WebDriver (probably with its find_element_by_xpath function).

If we take this Twitter page as an example, and inspect the element in Firebug:

enter image description here

Note that for Firebug to display this HTML, I needed to first click on, in the Style tab of the sidebar:

enter image description here

And the XPath for the anchor is:

/html/body/div[2]/div[2]/div/div[1]/div/div[2]/div/div/div[2]/div/div/ul/li[1]/a

From there, I would think I could simply tag on /@data-original-title to this Xpath, and any program that tries to fetch this would succeed. Yet both Webdriver's find_element_by_xpath nor Google Sheet's importXml() fail to retreive anything.

I know very little about Javascript or CSS, but I imagine that the fact that I needed to click on the "Inherited From" would suggest that one or both are involved in my challenge. If so, how so? Can such an XPath query ever succeed in a case like this? If not, how can the exact Tweets number be gotten?

@ Webdriver Experts: Is there a way of mimicking such a mouseover in order to retrieve this value?

Upvotes: 1

Views: 911

Answers (1)

Saifur
Saifur

Reputation: 16201

If you want to hover over and then get the count of tweets that would be a lot more unnecessary works. As a workaround I would simply use the following JavaScript and get the count.

str = "return $(\"[href*='TheEllenShow/following']\").attr('data-original-title');"
count = driver.execute_script(str)

On the other hand, the actual count of tweets is saved in an attribute. In that case you should be able to find the element and get the attribute on the element to get the count as shown bellow:

element = driver.find_element(By.CSS_SELECTOR, "[href*='TheEllenShow/following']")
attr = element.get_attribute("data-original-title")

Upvotes: 3

Related Questions