我似乎無法選擇正確的元素來觸發Selenium的懸停動作。我能夠使用cookie登錄,并且能夠看到頁面加載,然后向下滾動,但我一定是選擇了不正確的xpath元素,并且不能完成懸停動作以從所需的表格中獲取資料。
from selenium import webdriver
from selenium.webdriver.firefox import firefox_profile
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import numpy as np
import pandas as pd
import re
url = 'https://www.oddsportal.com/tennis/czech-republic/wta-ostrava/dodin-oceane-linette-magda-h4MPchI4/'/span>
fp = webdriver.FirefoxProfile('/Users/Frontwing/Library/Application Support/Firefox/Profiles/zku6ulmv.ProxyTest')
driver = webdriver.Firefox(executable_path="config/geckodriver", firefox_profile=fp)
driver.get(url)
items = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[7]/div[3]"/span>)
.get_attribute("innerHTML")
def get_cash_data() 。
# I. 通過懸停和收集來獲得原始資料"/html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[7]/div[3]"/span>
data = driver.find_element_by_xpath(xpath)
driver.execute_script("arguments[0].rollIntoView();", data)
hov = ActionChains(driver).move_to_element(data)
hov.執行()
data_in_the_bubble = driver.find_element_by_xpath(xpath)
hover_data = data_in_the_bubble.get_attribute("innerHTML"/span>)
# II. 提取開盤賠率
b = re.split('<br> ', hover_data)
#c = re.split('([^()]d ))', b)
#opening_odd = c
#print(open_odd)
print(hover_data)
return(b)
#print([x.text for x in items])
get_cash_data()
這里是我想懸停的目標的HTML片段:
<td class="right odds up"/span>> <div onm ouseout="delayHideTip()" onm ouseover="page。 hist(this,'E-0.00-0','55fmkx2s5a4x0xdlo2u',44,event,0,1)" style="display: block;"> 2. 42<br>(42)</div>< /td>
我想xpath可能是一個更容易確定元素的方法,但也許不是?下面是錯誤資訊:
檔案 "opstest.py",第44行,在 get_cash_data() selenium.common.exceptions.NoSuchElementException。訊息。無法定位的元素:.right odds
。特別是,我懷疑這個問題與xpath有關,也可能與hov作業流有關。我一定是做得不對,或者順序不對。
uj5u.com熱心網友回復:
我沒有登錄這里,但我可以看到所有的tr和td的,可能他們是不同的,但報廢的工具提示邏輯將保持不變。
browser。css而不是xpath.示例代碼:
driver = webdriver.Chrome(driver_path)
driver.maximum_window()
#driver.implicitly_wait(30)/span>
wait = WebDriverWait(driver, 50)
action = ActionChains(driver)
driver.get("https://www.oddsportal.com/tennis/czech-republic/wta-ostrava/dodin-oceane-linette-magda-h4MPchI4/")
first_td = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//tr[@class='lo odd']/td[2]"/span>))
ActionChains(driver).move_to_element(first_td).perform()
tool_tip_text_container = driver.find_element(By.CSS_SELECTOR, "span#tooltiptext").get_attribute('innerText')
print(tool_tip_text_container)
匯入 :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
輸出 :
20 Sep, 17:42 2. 30 0.03
20 Sep, 17:29 2.27 0.02
209月,17:25 2.25 -0.20
20 9月,17:24 2.45 0.20[/span
209月,17:20 2.25 -0.20
20 9月,17:19 2.45 0.20[/span
209月,16:58 2.25 -0.20
209月,16:56 2.45 0.20
209月,16:30 2.25 -0.20
209月,16:29 2.45 0.18
209月,16:23 2.27 -0.18
20 9月,16:21 2.45 0.20[/span
209月,15:52 2.25 -0.20
209月,15:51 2.45 0.20
20九月,15:45 2.25 -0.20
209月,15:42 2.45 0.20
20 9月,15:41 2.25 -0.20[/span
209月,15:36 2.45 0.12
20 Sep, 15:16 2.33 -0.12
20九月,15:14 2.45 0.12
開盤賠率。
19 Sep, 19: 00 2.33
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/327066.html
標籤:

