試圖從 fanduel 中獲取賠率,目標是獲得玩家的名字。在這種情況下,杰森·塔圖姆。
https://sportsbook.fanduel.com/basketball/nba/philadelphia-76ers-@-boston-celtics-31137202?tab=player-points
即使我直接從 chrome 復制 xpath,它似??乎也不起作用。盡管當我硬編碼并通過包含文本 Jayson Tatum 的 xpath 查找元素時它會起作用。這是我的代碼
name = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH,'//*[@id="root"]/div/div[2]/div[1]/div/div[2]/div[3]/div/div[2]/div/div[3]/div[1]/div/div/div[1]/span')))
也試過這個
name = driver.find_element(By.XPATH, '//*[@id="root"]/div/div[2]/div[1]/div/div[2]/div[3]/div/div[2]/div/div[3]/div[1]/div/div/div[1]/span')
仍然嘗試兩種方式都得到一個 NoSuchElement。
uj5u.com熱心網友回復:
要列印文本,Jayson Tatum您可以使用以下定位器策略:
使用xpath和text屬性:
print(driver.find_element(By.XPATH, "//span[text()='UNDER']//following::div[1]//span").text)
理想情況下,您需要為visibility_of_element_located()引入WebDriverWait,您可以使用以下任一定位器策略:
使用XPATH和
get_attribute("innerHTML"):print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='UNDER']//following::div[1]//span"))).get_attribute("innerHTML"))注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
您可以在How toret the text of a WebElement using Selenium - Python 中找到相關討論
參考
鏈接到有用的檔案:
get_attribute()方法Gets the given attribute or property of the element.text屬性回傳The text of the element.- 使用 Selenium 的 text 和 innerHTML 之間的區別
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/391997.html
