Skip to main content
 首页 » 编程设计

javascript之如何在 Python 中使用 Selenium 定位 onmouseover 元素

2025年05月04日25emanlee

我正在尝试抓取一个网站,有一个元素,如果您将鼠标移到它上面,它会在气泡中显示一些信息。我正在使用 Selenium 抓取页面,但我不知道如何定位特定元素。

查看页面的源代码,我得到了这个:

<td class="right odds up"><div onmouseout="delayHideTip()" onmouseover="page.hist(this,'P-0.00-0-0','1sj0oxv464x0x3pm6i',14,event,0,1)"> 

提供一些细节,以下是我要抓取的页面:match page .当您将鼠标移到箭头和数字上时,会出现一个包含一些内容的矩形。这就是我想要得到的。

请您参考如下方法:

我将按照以下步骤解决给定的问题:

from selenium import webdriver 
from selenium.webdriver.common.action_chains import ActionChains 
driver = webdriver.Firefox() 
driver.get("Your URL HERE") 

找到你的元素(你想要悬停的元素)

data = driver.find_element_by_xpath('//*[@id="odds-data-table"]/div[1]/table/tbody/tr[2]/td[4]') 

之后将鼠标悬停在元素上

hov = ActionChains(driver).move_to_element(data) 
hov.perform() 

获取数据

data_in_the_bubble = driver.find_element_by_xpath("//*[@id='tooltiptext']") 
hover_data = data_in_the_bubble.get_attribute("innerHTML")