簡體   English   中英

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

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

我正在嘗試抓取一個網站,並且有一個元素,如果您將鼠標移到它上面,它將在氣泡中顯示一些信息。 我正在使用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]')

之后,將on元素懸停

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")

您可以在Selenium代碼中使用Javascript。 在此處查看示例答案: 使用Python在Selenium中運行javascript

然后,使用Javascript,可以觸發onMouseOver事件,如該線程所示: 如何在未真正鼠標懸停時如何在元素上觸發鼠標懸停功能

觸發后,您將能夠找到新顯示的HTML內容並獲取其文本。

您可以使用xpath:

driver.find_elements(By.XPATH, '//*[@onmouseover]')

它將搜索所有定義了onmouseover屬性的元素。

警告,如果屬性是使用javascript使用addEventListener添加的,則無法使用

希望能有所幫助。

您有一個令人誤解的問題。 實際上,當您執行mouseover()操作時,您會錯過被數據填充的元素。

在頁面底部,您可以找到以下代碼:

<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>

首先,進行懸停操作,之后#tooltiptext元素將被填充。 這是簡單的定位器,可以使用:

tooltiptext = findElement(By.xpath("//*[@id='tooltiptext']");

BeautifulSoup可以做到這一點! 獲取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']

打印以下內容: 'bigImg(this)'

我認為這個問題是針對特定的網站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中有任何列值onmouseover屬性,它將對其進行檢測。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM