消息:元素点击被拦截:元素 <span>...</span> 在点 (657, 594) 不可点击。其他元素将收到 Selenium 的点击

Message: element click intercepted: Element <span>...</span> is not clickable at point (657, 594). Other element would receive the click with Selenium

我正在制作一个 webscraper 以从 https://www.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us 获取 gpu 库存以获取 30 系列卡,为此我正在使用 python 与 bs4 和 selenium。

我想加载更多购物项目,网站上有这个 load more button。所以我抓住了它的 class 并制作了它以便 selenium 点击它:

driver.find_element_by_class_name("buy-link").click()

但它表示该元素不可交互,HTML 用于按钮

它给我的确切错误是:

Message: element click intercepted: Element <span class="extra_style buy-link" data-color="#76b900" data-secondary-color="#fff" style="visibility: visible; cursor: pointer;" data-mpn-code="NVGFT070">...</span> is not clickable at point (657, 594). Other element would receive the click: <div class="popBg" id="nv-buy-now-model" style="display: block;">...</div>

不太了解HTML,如何实现点击这个按钮

加载更多 元素是 Angular element so to click on it you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following :

  • 使用CSS_SELECTOR:

    driver.get("https://www.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#cookiePolicy-btn-close>span"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.buy-link.load-more-btn[value='LOAD MORE']"))).click()
    
  • 使用XPATH:

    driver.get("https://www.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='cookiePolicy-btn-close']/span"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='buy-link load-more-btn' and @value='LOAD MORE']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:

问题是该页面上有大约 20 个元素与您的第一个定位器 .find_element_by_class_name("buy-link") 相匹配。在这种情况下,如果您使定位器更具体,则可以将其隔离为仅“加载更多”输入按钮。

试试这个

driver.find_element_by_css_selector("input.buy-link").click()

添加等待时间通常是一个好习惯,除非您确定永远不需要它。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='CLOSE']")).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.buy-link")).click()

有关更多信息和详细信息,请参阅 the docs