How can I locate a onmouseover element using Selenium in Python?
I would follow the following steps to solve the given problem:
from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsdriver = webdriver.Firefox()driver.get("Your URL HERE")
locate your element (which you want to hover)
data = driver.find_element_by_xpath('//*[@id="odds-data-table"]/div[1]/table/tbody/tr[2]/td[4]')
after that hover the on element
hov = ActionChains(driver).move_to_element(data)hov.perform()
and fetch the data
data_in_the_bubble = driver.find_element_by_xpath("//*[@id='tooltiptext']")hover_data = data_in_the_bubble.get_attribute("innerHTML")
You can use Javascript in your Selenium code. Check the answer here for an example: Running javascript in Selenium using Python
Then, using Javascript, you can trigger the onMouseOver event like shown in this thread: How to trigger mouseover function on an element when not really mouseovered
Once triggered you'll be able to locate the newly shown HTML content and get its text.
You can use xpath:
driver.find_elements(By.XPATH, '//*[@onmouseover]')
it will search all elements who have onmouseover attribute defined.
Waring, it won't work if the attribute is added by javascript with
addEventListener
Hope that helps.