简体   繁体   English

ElementClickInterceptedException:消息:使用 Selenium WebDriver 拦截元素点击

[英]ElementClickInterceptedException: Message: element click intercepted using Selenium WebDriver

I am trying to click on a field, while running automated test but I get this error:我试图在运行自动化测试时单击一个字段,但出现此错误:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input id="artistNominee010" type="checkbox" class="rock-artist-checkbox"> is not clickable at point (967, 601). Other element would receive the click: <span class="rock-artist-toggle-icon"></span>

Here is my line of code that tries to access the attribute:这是我尝试访问该属性的代码行:

select_nominee = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.ID, 'artistNominee010'))).click()

This is the DOM structure of the page:这是页面的 DOM 结构:

<input id="artistNominee010" type="checkbox" class="rock-artist-checkbox">
<label class="rock-artist-label" for="artistNominee010">
<span class="rock-artist-photo-wrap">
<img class="rock-artist-photo" alt="" src="/images/artist-photos/nominee010.jpg"></span>
<span class="rock-desktop-hide rock-artist-name">John Doe</span>
<span class="rock-accessible-text">select John Doe</span>
<span class="rock-artist-toggle-icon"></span></label>

From the site, this is the element that is clickable, but i can't select it directly as other elements contain the same class name with different nominees, the id is unique.从站点来看,这是可点击的元素,但我不能直接 select 它,因为其他元素包含相同的 class 名称和不同的被提名者,ID 是唯一的。

<span class="rock-accessible-text">select John Doe</span>

is there anyway to do something like this:反正有没有做这样的事情:

select_nominee = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.ID, 'artistNominee010').class('rock-accessible-text')).click()

Instead of presence, wait for the element to be clickable而不是存在,而是等待元素可点击

select_nominee = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.ID, 'artistNominee010'))).click()

Because the element can be present in the DOM, but not yet clickable (if obstructed by another element, which is probably in your case).因为该元素可以存在于 DOM 中,但还不能点击(如果被另一个元素阻挡,这可能是您的情况)。

To click on the element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :要单击元素而不是 present_of_element_located presence_of_element_located()您需要为element_to_be_clickable()诱导WebDriverWait并且您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='artistNominee010']"))).click()
  • Using XPATH :使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='artistNominee010']"))).click()
  • Note : You have to add the following imports:注意:您必须添加以下导入:

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

So I was able to fix this, in a very simple way, as shown below:所以我能够以一种非常简单的方式解决这个问题,如下所示:

from selenium.webdriver.common import keys

select_nominee = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'artistNominee010'))).send_keys(keys.Keys.SPACE)

for reference visit Selenium: Element not clickable … Other Element Would Receive Click供参考访问Selenium:元素不可点击...其他元素将收到点击

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ElementClickInterceptedException:消息:元素点击被拦截: - ElementClickInterceptedException: Message: element click intercepted: ElementClickInterceptedException: 消息: 元素点击被拦截: <label>Selenium 和 Python 无法点击</label>元素 - ElementClickInterceptedException: Message: element click intercepted: Element <label> is not clickable with Selenium and Python Python & Selenium:ElementClickInterceptedException:消息:元素点击拦截错误 - Python & Selenium: ElementClickInterceptedException: Message: element click intercepted error selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截 - selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted selenium.common.exceptions.ElementClickInterceptedException:消息:使用 Selenium 单击单选按钮时元素单击被截获错误 - selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted error clicking on a radiobutton using Selenium Python ElementClickInterceptedException:消息:元素单击拦截元素不可单击错误单击使用 Selenium 和 Python 的单选按钮 - ElementClickInterceptedException: Message: element click intercepted Element is not clickable error clicking a radio button using Selenium and Python selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素不可点击 Selenium 和 Python - selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python 为什么得到这个 selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element - Why getting this selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ElementClickInterceptedException:元素点击被拦截:使用 Selenium Python 单击搜索按钮时,元素不可点击 - ElementClickInterceptedException: element click intercepted: Element is not clickable at point error clicking on Search button using Selenium Python ElementClickInterceptedException:消息:元素单击被拦截 - 如何在 for 循环中定位并切换到 iframe - ElementClickInterceptedException: Message: element click intercepted - how to locate and switch to iframe in a for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM