简体   繁体   中英

How can I locate a onmouseover element using Selenium in Python?

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 would follow the following steps to solve the given problem:

from selenium import webdriver
from selenium.webdriver.common.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.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']

Prints the following: 'bigImg(this)'

I think this question refers to a specific website oddsportal.com. 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.

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