简体   繁体   中英

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 eg 12.4K. When you move the mouse over this element, a tooltip tells you the true number eg 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:

在此处输入图片说明

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

在此处输入图片说明

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?

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")

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