0

I want to use the click() function of selenium on these buttons in the following manner(the first two clicks):[]

For reference the 1st button I want to click and its code:


[]

2nd button with its code:
[]

I tried using driver.find_element(By.XPATH,"//button[contains(@type,\"button\")]").click() on the first button (the "X" mark) but it didn't work and got this error:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button type="button" class="el-button yellowBtn el-button--default">...</button> is not clickable at point (606, 507). Other element would receive the click: <img src="https://d2g38dx0j6xqav.cloudfront.net/online/img/app-inner/popup.png" alt="" style="width: 100%;">

If I somehow get it to click the first button then getting it to click the second button will be much easier.

Shawn
  • 4,064
  • 2
  • 11
  • 23

4 Answers4

0

I think you should wait until the element is visible in the modal dialog box. U can use both the Explicit and Implicit waits methods for the above problem.

pr96
  • 994
  • 5
  • 17
  • instead of these, can the `sleep` function be used or both (sleep and wait method) have different purpose? – Denied Access Feb 10 '23 at 16:12
  • @DeniedAccess Using sleep is a bad practice because it's not "smart". It can't wait until something is available, it only waits the specified amount of time. If you wait for 5s and the element isn't available, it still fails. If you want for 30s and it's available in 1s, you've wasted a lot of time waiting when you didn't need to. The best practice is to use WebDriverWait and wait for the element to be clickable, visible, etc. using ExpectedConditions. Do not use implicit waits. The Selenium lead and devs have said many times that it should not be used. – JeffC Feb 10 '23 at 16:44
  • @pr96 Waiting for the element underneath to be clickable won't solve this problem. Selenium doesn't know that there's an element above it so it will be clickable but the click will still be intercepted. – JeffC Feb 10 '23 at 16:45
0

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted

Above exception implies that the element you are trying to action upon isn't clickable, as some other element is overshadowing it.

Try by inducing wait(see below):

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"Your XPath expression here"))).click()

if the above doesn't work, try using execute_script() method(see below):

driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "Your XPath expression here"))))

Required imports statements:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Shawn
  • 4,064
  • 2
  • 11
  • 23
  • Waiting for the element underneath to be clickable won't solve this problem. Selenium doesn't know that there's an element above it so it will be clickable but the click will still be intercepted. – JeffC Feb 10 '23 at 16:42
0

Always read exception and/or error messages carefully to see what you can learn about the problem. In this case, it tells you exactly what happened. I reformatted and shortened it to make it more readable.

ElementClickInterceptedException:
Message: element click intercepted: Element <button...> is not clickable.
Other element would receive the click: <img...>

Selenium is telling you that it tried to click the BUTTON but the click was intercepted (it never got clicked because there was another element covering it). The element that got clicked instead was an IMG.

Without seeing the full HTML of the page it's hard to say but your BUTTON locator looks pretty generic and there's a good chance that you clicked a different BUTTON than you intended. I would use the locator below instead to click on the I tag.

wait = WebDriverWait(driver, 10)
close_icon = (By.CSS_SELECTOR, "i.el-dialog__close")
wait.until(EC.element_to_be_clickable(close_icon)).click() # click the close icon
wait.until(EC.invisibility_of_element_located(close_icon)) # wait for the close icon to disappear indicating the dialog is fully closed
# continue script

If that still doesn't work, you will probably need to use a JS click. If you are writing UI tests, then you should avoid JS clicks unless no other solution can be found because it allows the script to do things that a user can't. If you aren't writing tests, then it likely doesn't matter.

wait = WebDriverWait(driver, 10)
close_icon = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "i.el-dialog__close")))
driver.execute_script("arguments[0].click()", close_icon)
# continue script
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • i got this error which is almost the same error i got using others mentioned solutions: https://pastebin.com/tda3VMTE. If you want I'll link the whole HTML code. – Denied Access Feb 11 '23 at 03:09
  • That error is not coming from my code. Did you try my code? Is the error happening after my code runs or ? – JeffC Feb 11 '23 at 04:32
  • Yes I did try your code and since your code is similar with the ones others have mentioned here, I'm getting the same type of error. I Cannot understand much even by reading the error. – Denied Access Feb 11 '23 at 07:15
  • The error you posted above was from an XPath but my answer is using CSS selectors. I can't help you debug my code unless you run it and then post any error you are receiving and also indicate on which line the error is being thrown. e.g. is it coming from my code or previous/later code. – JeffC Feb 11 '23 at 15:31
  • I think we should let this disintegrate, I found an alternative solution instead of using selenium which is to use the mouse module on pypi where I just have to specify coordinates of the button and it will click on it. I appreciate and respect your time and efforts you put just for my question. – Denied Access Feb 12 '23 at 04:50
0

You desired element is:

element

But your line of code:

driver.find_element(By.XPATH,"//button[contains(@type,\"button\")]").click()

is attempting to click on:

<button type="button" class="el-button yellowBtn el-button--default">...</button>

which isn't the desired element and still the click attempt is intercepted by:

<img src="https://d2g38dx0j6xqav.cloudfront.net/online/img/app-inner/popup.png" alt="" style="width: 100%;">

Solution

To click element 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, "button.el-dialog__headerbtn[aria-label='Close'] > i.el-dialog__close.el-icon.el-icon-close"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='el-dialog__headerbtn' and @aria-label='Close']/i[@class='el-dialog__close el-icon el-icon-close']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352