gpt4 book ai didi

python - 为什么在 Selenium 中等待元素加载后元素仍未出现?

转载 作者:行者123 更新时间:2023-12-04 07:14:51 25 4
gpt4 key购买 nike

我有一个我想抓取的特定 URL,但是,某个元素在页面完全加载之前不会加载,我无法让 Selenium 找到它:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome(Insert Driver Path Here)
game_url = "https://rbsdm.com/stats/box_scores/?_inputs_&type=%22reg%22&away=%22ARI%22&home=%22SF%22&year=2020"
driver.get(game_url)
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "table table-striped table-hover table-responsive"))
)
print(element)

<div>有我想要的表的 ID qbs ,然而,它显然在初始加载时不包含任何内容。该表具有我在制作 element 时使用的类名变量。

页面的数据肯定是在我给它的 10 秒窗口后加载的,所以我很困惑这不起作用。

最佳答案

在您的代码中,当您搜索所需的 HTML 元素时:

element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "table table-striped table-hover table-responsive"))
)

您正在寻找一个类名,但是table table-striped table-hover table-responsive 不是一个类名,它是一个 CSS 选择器(带链接带点的空格 .):

table.table-striped.table-hover.table-responsive

所以,要么使用上面的 CSS 选择器(注意 By.CSS_SELECTOR):

element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "table.table-striped.table-hover.table-responsive"))
)

或者使用类名:

element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "table"))
)

这是一个完整的工作示例:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait


driver = webdriver.Chrome()
game_url = "https://rbsdm.com/stats/box_scores/?_inputs_&type=%22reg%22&away=%22ARI%22&home=%22SF%22&year=2020"
driver.get(game_url)
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "table"))
)
print(element.text)

输出:

Player Plays EPA/play Total EPA aDoT CMP% xCMP% CPOE xQBR
K.Murray 54 0.18 9.9 5.2 70.3 72.5 -2.2 63.6
J.Garoppolo 38 0.1 3.6 7 59.4 65.6 -6.2 54.9

关于python - 为什么在 Selenium 中等待元素加载后元素仍未出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68824470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com