Michael Ajanaku
Michael Ajanaku

Reputation: 95

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:

<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.

<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()

Upvotes: 0

Views: 4644

Answers (3)

Michael Ajanaku
Michael Ajanaku

Reputation: 95

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

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193108

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:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='artistNominee010']"))).click()
    
  • Using 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
    

Upvotes: 0

Mate Mrše
Mate Mrše

Reputation: 8394

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).

Upvotes: 1

Related Questions