admin管理员组文章数量:1299979
I am trying to scrape a site and there is an element that if you move your mouse over it it displays some information in a bubble. I am using Selenium to scrape the page but I don't know how to locate the specific element.
Looking at the source of the page I get this:
<td class="right odds up"><div onmouseout="delayHideTip()" onmouseover="page.hist(this,'P-0.00-0-0','1sj0oxv464x0x3pm6i',14,event,0,1)">
Providing some details, the following is the page I want to scrape: match page. When you move your mouse over the arrows and the numbers a rectangle appears with some content. That's what I want to get.
I am trying to scrape a site and there is an element that if you move your mouse over it it displays some information in a bubble. I am using Selenium to scrape the page but I don't know how to locate the specific element.
Looking at the source of the page I get this:
<td class="right odds up"><div onmouseout="delayHideTip()" onmouseover="page.hist(this,'P-0.00-0-0','1sj0oxv464x0x3pm6i',14,event,0,1)">
Providing some details, the following is the page I want to scrape: match page. When you move your mouse over the arrows and the numbers a rectangle appears with some content. That's what I want to get.
Share Improve this question edited Apr 24, 2015 at 20:42 IordanouGiannis asked Apr 19, 2015 at 0:32 IordanouGiannisIordanouGiannis 4,35716 gold badges67 silver badges100 bronze badges 3-
you can just call
elm.onmouseover()
to fire it, no need to pretend. – dandavis Commented Apr 19, 2015 at 1:06 - I don't understand what you mean. Can you explain it ? – IordanouGiannis Commented Apr 19, 2015 at 1:10
-
2
Do you mean locating the element that has the
onmouseover
event handler, or locating the element that appears whenonmouseover
is triggered on the element that has the handler? – Louis Commented Apr 24, 2015 at 11:00
6 Answers
Reset to default 8 +50I would follow the following steps to solve the given problem:
from selenium import webdriver
from selenium.webdriver.mon.action_chains import ActionChains
driver = 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.
You have a little bit misleading question. In fact, you miss the element which is getting filled by data, when you do mouseover() action.
At the bottom of the page you can find following code:
<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>
First, make a hover action, the #tooltiptext element gets filled after. This is simple locator, you can use:
tooltiptext = findElement(By.xpath("//*[@id='tooltiptext']");
BeautifulSoup can do this! Take the HTML and dump it to a soup...
from bs4 import BeautifulSoup
import requests
r = requests.get("http://www.w3schools./jsref/tryit.asp?filename=tryjsref_onmouseover")
theHtml = r.content
theSoup = BeautifulSoup(theHtml)
for event_tag in theSoup.findAll(onmouseover=True):
print event_tag['onmouseover']
Prints the following: 'bigImg(this)'
I think this question refers to a specific website oddsportal.. If anyone wants to get initial odds, which shows up when you put your mouse on any bookmaker odd, you can use the following:
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")
This code iterates through all bookmaker odds, gets all their column values. If any column value onmouseover attribute in its div, it detects it.
本文标签: javascriptHow can I locate a onmouseover element using Selenium in PythonStack Overflow
版权声明:本文标题:javascript - How can I locate a onmouseover element using Selenium in Python? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741650778a2390462.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论