Javascript 如何在Python中使用Selenium定位onmouseover元素?
我正试图刮一个网站,有一个元素,如果你把鼠标移到它上面,它会在一个气泡中显示一些信息。我正在使用Selenium来抓取页面,但我不知道如何定位特定元素 查看页面的来源,我发现:Javascript 如何在Python中使用Selenium定位onmouseover元素?,javascript,python,selenium,Javascript,Python,Selenium,我正试图刮一个网站,有一个元素,如果你把鼠标移到它上面,它会在一个气泡中显示一些信息。我正在使用Selenium来抓取页面,但我不知道如何定位特定元素 查看页面的来源,我发现: <td class="right odds up"><div onmouseout="delayHideTip()" onmouseover="page.hist(this,'P-0.00-0-0','1sj0oxv464x0x3pm6i',14,event,0,1)"> 提供了一些详细信息,以
<td class="right odds up"><div onmouseout="delayHideTip()" onmouseover="page.hist(this,'P-0.00-0-0','1sj0oxv464x0x3pm6i',14,event,0,1)">
提供了一些详细信息,以下是我想要抓取的页面:。当您将鼠标移到箭头和数字上时,会出现一个带有某些内容的矩形。这就是我想要的。您可以在Selenium代码中使用Javascript。请检查此处的答案以获取示例: 然后,使用Javascript,您可以触发onMouseOver事件,如以下线程所示:
触发后,您将能够找到新显示的HTML内容并获取其文本。您可以在Selenium代码中使用Javascript。请检查此处的答案以获取示例: 然后,使用Javascript,您可以触发onMouseOver事件,如以下线程所示: 触发后,您将能够找到新显示的HTML内容并获取其文本。您可以使用xpath:
driver.find_elements(By.XPATH, '//*[@onmouseover]')
它将搜索定义了onmouseover属性的所有元素
警告,如果该属性是由带有addEventListener的javascript添加的,那么它将不起作用
希望对您有所帮助。您可以使用xpath:
driver.find_elements(By.XPATH, '//*[@onmouseover]')
它将搜索定义了onmouseover属性的所有元素
警告,如果该属性是由带有addEventListener的javascript添加的,那么它将不起作用
希望这能有所帮助。你有一个有点误导性的问题。事实上,当您执行鼠标悬停操作时,您错过了由数据填充的元素 在页面底部,您可以找到以下代码:
<div id="tooltipdiv">
<span class="help">
<span class="help-box3 y-h wider">
<span class="wrap-help">
<span class="spc" id="tooltiptext">
... onmouseover() text goes here..
</span>
</span>
</span>
</span>
</div>
你的问题有点误导人。事实上,当您执行鼠标悬停操作时,您错过了由数据填充的元素 在页面底部,您可以找到以下代码:
<div id="tooltipdiv">
<span class="help">
<span class="help-box3 y-h wider">
<span class="wrap-help">
<span class="spc" id="tooltiptext">
... onmouseover() text goes here..
</span>
</span>
</span>
</span>
</div>
我能做到!获取HTML并将其转储到汤中
from bs4 import BeautifulSoup
import requests
r = requests.get("http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onmouseover")
theHtml = r.content
theSoup = BeautifulSoup(theHtml)
for event_tag in theSoup.findAll(onmouseover=True):
print event_tag['onmouseover']
打印以下内容:“bigImgthis”可以做到这一点!获取HTML并将其转储到汤中
from bs4 import BeautifulSoup
import requests
r = requests.get("http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onmouseover")
theHtml = r.content
theSoup = BeautifulSoup(theHtml)
for event_tag in theSoup.findAll(onmouseover=True):
print event_tag['onmouseover']
打印以下内容:“bigImgthis”我将按照以下步骤解决给定问题:
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")
我将按照以下步骤解决给定的问题:
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")
我想这个问题是指一个特定的网站oddsportal.com。 如果任何人想要获得初始赔率,当你将鼠标放在任何一个博彩公司的奇数上时,就会出现这种情况,你可以使用以下方法:
table = bookie_data.find('table', {'class': "table-main detail-odds sortable"}) # Find the Odds Table
# This part is scraping a Beautiful Soup Table. Returns the odds and the bookie name for the match
table_body = table.find('tbody')
rows = table_body.find_all('tr') # rows are different bookmakers
for row in rows: # for each bookmaker
cols = row.find_all('td')
for event_tag in cols:
# if it has onmouseover attribute
if event_tag.find("div", onmouseover=True) is not None:
# do stuff here
event_tag.find("div", onmouseover=True).get("onmouseover")
这段代码遍历所有博彩赔率,获取它们的所有列值。如果在其div中有mouseover属性上的任何列值,则会检测到它 我想这个问题是指一个特定的网站oddsportal.com。 如果任何人想要获得初始赔率,当你将鼠标放在任何一个博彩公司的奇数上时,就会出现这种情况,你可以使用以下方法:
table = bookie_data.find('table', {'class': "table-main detail-odds sortable"}) # Find the Odds Table
# This part is scraping a Beautiful Soup Table. Returns the odds and the bookie name for the match
table_body = table.find('tbody')
rows = table_body.find_all('tr') # rows are different bookmakers
for row in rows: # for each bookmaker
cols = row.find_all('td')
for event_tag in cols:
# if it has onmouseover attribute
if event_tag.find("div", onmouseover=True) is not None:
# do stuff here
event_tag.find("div", onmouseover=True).get("onmouseover")
这段代码遍历所有博彩赔率,获取它们的所有列值。如果在其div中有mouseover属性上的任何列值,则会检测到它 你可以打电话给elm.onmouseover来开它,不用假装。我不明白你的意思。你能解释一下吗?你的意思是定位有onmouseover事件处理程序的元素,还是定位在有onmouseover事件处理程序的元素上触发onmouseover时出现的元素?你可以调用elm.onmouseover来启动它,不需要假装。我不明白你的意思。您能解释一下吗?您的意思是定位具有onmouseover事件处理程序的元素,还是定位在具有该处理程序的元素上触发onmouseover时出现的元素?