Selenium 和 Python 中的 ElementClickInterceptedException 解决方案

ElementClickInterceptedException Solutions in Selenium and Python

我打算自动按下网站上的一个按钮,但与此同时,一个叫做 campaigns(kampanyalar) 的东西阻止了它。我该如何解决这个问题?我希望其他元素不被点击 属性

错误:

ElementClickInterceptedException: element click intercepted: Element <div class="styled__CampaignCardDescriptionTitle-sc-1n4y3hk-2 hulWUi" color="">...</div> is not clickable at point (531, 35). Other element would receive the click: <a class="styled__HeaderMenuItem-sc-126ws66-35 gEoUee" href="/kampanyalar" id="DPE_TR_HOME_BUTTON_HEADERITEM0">...</a>

ElementClickInterceptedException 表示您要与之交互的元素在 Selenium 视图端口中不可用。

3 件事 :

1. 全屏启动浏览器:

driver.maximize_window()

2. 使用 execute_script 滚动到该元素:

driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_css_selector(.your_css_selector))

3. ActionChains的使用:

ActionChains(driver).move_to_element(driver.find_element_by_id('some id where you want to go')).perform()

导入 你需要:

from selenium.webdriver.common.action_chains import ActionChains

ElementClickInterceptedException 可能由多种原因引起。
最厉害的是用JavaScript点击它,而不是用webdriver点击。像下面这样:

element = driver.find_element_by_xpath("xpath")
driver.execute_script("arguments[0].click();", element)

不过最好看看是什么原因导致了这个异常。
如果元素超出可见屏幕或某些页脚元素与其重叠,最好将该元素滚动到视图中:

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_xpath("xpath")
driver.execute_script("arguments[0].scrollIntoView();", element)

在某些其他情况下,会发生这种情况,因为您试图在页面仍在加载元素时单击元素,因为当前在该元素上找到了其他元素。在这种情况下,最好等到这个元素变得可点击。像这样:

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

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "xpath"))).click()

因此,最好根据您所面临的具体情况使用更多类似用户的操作,并且只有在没有其他选择时才使用 JavaScript clicks