首页 > 解决方案 > 我对 selenium 以及如何使用 CSS 选择器和一个错误有一些疑问:'selenium.common.exceptions.ElementClickInterceptedException'

问题描述

我做了一些挖掘,我想我了解 CSS 选择器是什么以及它们的外观,但是使用 selenium,我如何在站点中搜索 CSS 选择器然后单击它?我真的不知道语法应该是什么样子。

我也收到了这个错误:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: 
Element <div class="echo-component-EchoButton-3T1Xv">...</div> is not clickable at point (95, 328). 
Other element would receive the click: <div id="background" style="opacity: 1; overflow-y: auto; position: 
absolute; top: 0px; left: 0px; height: 100%; width: 100%; transition: opacity 0.3s ease 0s; display: flex; 
align-items: baseline;">...</div>

根据我的阅读,“将接收点击的其他元素”看起来像一个 CSS 选择器,所以这就是为什么我想知道如何使用它们并能够与它们交互。我是 selenium 的新手,正在阅读 HTML 和 CSS,如果我错了,请纠正我。任何帮助将非常感激!

标签: pythonhtmlcsspython-3.xselenium

解决方案


据我所知, Element <div class="echo-component-EchoButton-3T1Xv">...</div> is not clickable at point (95, 328). 这可能意味着该元素尚未准备好被点击。这可以通过在单击之前给元素一些时间来解决。

您可以尝试阅读或使用这些:

WebDriverWait 
sleep

两者都将为元素准备好一些时间。

对于selenium.common.exceptions.ElementClickInterceptedException,我通常会在有问题的区域中包含一些异常处理。可以在这里阅读:https://www.guru99.com/exception-handling-selenium.html

我通常做的例子:

try:
    wait.until(EC.visibility_of_element_located(
        (By.CSS_SELECTOR, ".fr66n .\\_8-yf5"))).click()

except TimeoutException:
    next_button = self.driver.find_element_by_link_text("Next")
    next_button.click()

else:
    sleep(1)
    next_button = self.driver.find_element_by_link_text("Next")
    next_button.click()

您也可以尝试使用 selenium IDE。真的帮助我更好地理解这个过程。


推荐阅读