在執行懸停操作后,我無法找到似乎始終存在的包含工具提示文本的 div。
我是網路抓取的新手,并嘗試使用 selenium 從博彩網站獲取一些賠率資料。我想提取每個單元格出現的工具提示資料。
我嘗試了以下代碼,但我超時了。如果我只是尋找 tooltiptext 元素,find_element那么我會得到一個 element not found 錯誤。我遇到的問題是我可以在 chrome 的檢查器中看到這個元素,但似乎無法在頁面中找到它。
#set up webdriver
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# initialize the Chrome driver
wd = webdriver.Chrome('chromedriver',options=chrome_options)
#get the website data
wd.get("https://www.oddsportal.com/soccer/argentina/primera-nacional/quilmes-san-
martin-t-n791ygOf/")
wd.maximize_window()
#Hover over element and get tooltip text
element = wd.find_element(by = By.XPATH, value='//*[@id="odds-data-
table"]/div[1]/table/tbody/tr[1]/td[2]')
hover = ActionChains(wd).move_to_element(element)
hover.perform()
tooltipText = WebDriverWait(wd, 10).until(EC.presence_of_element_located((By.XPATH,
'//*[@id="tooltiptext"]'))).text
請參見工具提示 div 的下圖:

uj5u.com熱心網友回復:
您必須點擊接受 cookie。
使用 innerText 而不是帶有顯式等待的文本。
代碼:
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.get("https://www.oddsportal.com/soccer/argentina/primera-nacional/quilmes-san-martin-t-n791ygOf/")
#Hover over element and get tooltip text
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
except:
pass
element = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='odds-data-table']/div[1]/table/tbody/tr[1]/td[2]")))
ActionChains(driver).move_to_element(element).perform()
tooltipText = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div#tooltipdiv"))).get_attribute('innerText')
print(tooltipText)
進口:
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
輸出:
04 Apr, 23:52 2.68 0.02
04 Apr, 23:44 2.66 0.05
04 Apr, 22:37 2.61 0.03
04 Apr, 22:28 2.58 0.04
04 Apr, 21:44 2.54 0.02
04 Apr, 20:00 2.52 -0.16
04 Apr, 19:51 2.68 0.15
04 Apr, 18:40 2.53 0.01
04 Apr, 17:31 2.52 -0.01
04 Apr, 13:56 2.53 -0.01
04 Apr, 12:16 2.54 -0.06
04 Apr, 12:15 2.60 0.14
04 Apr, 12:03 2.46 -0.02
04 Apr, 11:28 2.48 0.04
04 Apr, 05:26 2.44 -0.16
04 Apr, 00:32 2.60 0.01
03 Apr, 16:36 2.59 0.02
Opening odds:
29 Mar, 13:51 2.57
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/457289.html
