1

I'm using Selenium for testing. I want to click on an element. The element is very much clickable and visible, but it happens that the middle point of the element is obscured, causing the error.

Here is a MCVE:

HTML code (link to demo):

<style>
    button {
        width: 90vw;
        height: 90vh;
        position: fixed;
        top: 5vh;
        left: 5vw;
    }

    .cover {
        background: grey;
        opacity: 0.3;
        width: 80vw;
        height: 80vh;
        position: fixed;
        top: 10vh;
        left: 10vw;
    }
</style>

<button onclick="alert('hi');">
  Click me!
</button>

<div class="cover">
  I'm in the way!
</div>

Python selenium code:

from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://blissfulpreciouslanservers--five-nine.repl.co/")
button = driver.find_element_by_tag_name("button")
button.click()

Result:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button onclick="alert('hi');">...</button> is not clickable at point (451, 450). Other element would receive the click: <div class="cover">...</div>

This seems like a rather sad limitation of Selenium. The button is clickable, just not at all points. I don't want to have to fiddle with scrolling and coordinates.

There are many similar questions about the exception in general, e.g:

However the questions are never specifically about an element that is only partially obscured, so I haven't managed to find a proper answer to my own question. The answers to other questions generally fall into these categories:

  1. Wait until the element is clickable. Doesn't apply here.
  2. Use action chains, e.g. ActionChains(driver).move_to_element(button).click().perform(). This doesn't help because .move_to_element() moves to the middle of the element.
  3. Use JavaScript to perform the click. It seems like I might have to resort to this, but it's very unsatisfactory. I still want to validate that the element is clickable at least somewhere and not bypass all checks.
Alex Hall
  • 34,833
  • 5
  • 57
  • 89

1 Answers1

-1

Have you tried to add a class to your button and then search for the class? For example:

<button class="btn" onclick="alert('hi');">
  Click me!
</button>

Then use the following to find and click on that button:

driver.findElement(By.className("btn")).click();

similar to this stack overflow response by alecxe

Lemon
  • 19
  • 3