70

I know this has been asked lots of times before but how do you get around the "element not interactable" exception?

Here is my code:

button = driver.find_element_by_class_name(u"infoDismiss")
type(button)
button.click()
driver.implicitly_wait(10)

Here is the HTML:

<button class="dismiss infoDismiss">
    <string for="inplay_button_dismiss">Dismiss</string>
</button>

And here is the error message:

selenium.common.exceptions.ElementNotInteractableException: Message: 

After is says message there is literally nothing.

I have spent lots of time searching the web, not finding anything that solves my issue. I would really appreciate an answer.

Edit: Changed "w" to driver so it is easier to read

Update: I have just realized that I've found the HTML of the wrong button! The real button HTML is below:

<button class="dismiss">
    <string for="exit">Dismiss</string>
</button>

Also, I've used the answers and comments and edited my code to look like this:

button = driver.find_element_by_css_selector("button.dismiss")
w.implicitly_wait(10)
ActionChains(w).move_to_element(button).click(button)

And now I get a new error:

selenium.common.exceptions.WebDriverException: Message: Tried to run command without establishing a connection

The error happens in line 1: button = driver.find_element_by_css_selector("button.dismiss")

starball
  • 20,030
  • 7
  • 43
  • 238
Rolodophone
  • 813
  • 1
  • 7
  • 10

18 Answers18

55

A possibility is that the element is currently unclickable because it is not visible. Reasons for this may be that another element is covering it up or it is not in view, i.e. it is outside the currently view-able area.

Try this

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

button = driver.find_element(By.CLASS_NAME, u"infoDismiss")
driver.implicitly_wait(10)
ActionChains(driver).move_to_element(button).click(button).perform()
EyuelDK
  • 3,029
  • 2
  • 19
  • 27
  • I've tried that but it still doesn't seem to click it. It doesn't click it but it doesn't raise an error either. – Rolodophone Jun 01 '17 at 12:16
  • I suggest you check if it is ACTUALLY being clicked. Add some click event listeners on the element to see if a click is being performed instead of visually inspecting a click. – EyuelDK Jun 01 '17 at 14:39
  • 1
    Rolodophone, in that situation you describe, please try ActionChains(driver).move_to_element(button).click(button).perform(), that is, add ".perform()". – apascualb Aug 13 '19 at 10:01
  • @apascualb made the correction, thanks for the heads up. – EyuelDK Aug 14 '19 at 08:47
  • I am facing a similar problem, my question raised here: https://stackoverflow.com/questions/63316796/selenium-python-click-get-data-button/63317075#63317075 . button=driver.find_element_by_xpath("//*[@id='submitMe']") driver.implicitly_wait(10) ActionChains(driver).move_to_element(button).click(button).perform() I get error Message: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite. – Invictus Aug 08 '20 at 15:53
  • It can also be outside of sight because the browser window is too small. Try `options.add_argument("--disable-notifications")` – Nicolas Gervais Sep 15 '21 at 17:58
29

I just ran into a similar issue and was able to fix it by waiting until the button was "clickable".

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

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('useAutomationExtension', False)
browser = webdriver.Chrome('./chromedriver', options=chrome_options)

browser.get(('YOURWEBSITEHERE.COM'))

button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.dismiss')))
button.click()
  • I would name this a "work around" then a solution. But, well... :-D – zypro Jul 20 '19 at 14:52
  • 1
    Where does the variable `browser` come from? – Douy789 Jun 06 '20 at 11:53
  • 2
    @Douy789 I edited the comment to answer your question – Adam Polak Moetsi Jul 09 '20 at 14:02
  • 3
    @zypro I do not think it is a "work around", this waits until the button is interactable (clickable) which is the issue in the posted situation :) – Adam Polak Moetsi Jan 31 '21 at 17:31
  • 1
    Good Answer, not a workaround! - I tried many things before reading this post. In my case the element is definitely becoming visible before it is clickable, presumably because I only just revealed it with the previous click. – Giles Knap Mar 13 '22 at 18:36
  • should be the accepted answer! Seems to be actually the right way. Not waiting for some random time but rather wait until it _is_ clickable. – laka Aug 07 '23 at 20:27
21

For those discovering this now and the above answers didn't work, the issue I had was the screen wasn't big enough. I added this when initializing my ChromeDriver, and it fixed the problem:

options.add_argument("window-size=1200x600")
robbrit
  • 17,560
  • 4
  • 48
  • 68
17

The error "Message: element not interactable" mostly occurs when your element is not clickable or it is not visible yet, and you should click or choose one other element before it. Then your element will get displayed and you can modify it.

You can check if your element is visible or not by calling is_displayed() method like this:

print("Element is visible? " + str(element_name.is_displayed()))
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
romin parvardeh
  • 189
  • 1
  • 4
9

right-click and copy full xpath like below example it will work for sure

driver.find_element(By.XPATH, "/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]").click()
balu k
  • 3,515
  • 2
  • 14
  • 29
Prosenjit Bari
  • 117
  • 1
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 09 '21 at 15:15
  • This really works thak you so much – Gülsen Keskin Dec 10 '21 at 11:38
  • This also worked for me (actually, with the relative xpath). Does anyone have an explanation why xpath does the trick in these cases? – Clang Apr 22 '22 at 14:16
  • I _hate_ this solution. Unfortunately... it worked. **Edit**: The reason this solution worked, was because I was picking the _wrong_ element with my CSS selector. Guys, this solution is a good sanity-test, but if it works, make sure you can specify the element better in your CSS selector to see if you can find it. – Luiz Martins Jul 17 '23 at 06:08
8

I honestly did it via importing it from a library:

from selenium.webdriver.common.keys import Keys

search.send_keys(Keys.RETURN) 

I spent much time on it, maximizing the button, importing timer 'wait' but somehow nothing worked except this one

I am an amateur still but I wanted to contribute here with my solution : )

MD Mushfirat Mohaimin
  • 1,966
  • 3
  • 10
  • 22
Nick Panic
  • 83
  • 1
  • 6
2

Found a workaround years later after encountering the same problem again - unable to click element even though it SHOULD be clickable. The solution is to catch ElementNotInteractable exception and attempt to execute a script to click the element.

Example in Typescript

async clickElement(element: WebElement) {
    try {
        return await element.click();
    } catch (error) {
        if (error.name == 'ElementNotInteractableError') {
            return await this.driver.executeScript((element: WebElement) => {
                element.click();
            }, element);
        }
    }
}
EyuelDK
  • 3,029
  • 2
  • 19
  • 27
0

use id of the element except x_path.It will work 100%

GHULAM NABI
  • 498
  • 5
  • 15
0

First search for the element by xpath:

    buttonNoInteractable = browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div/div/div[2]/div/table[2]/thead/tr/th[2]/input')

Then Wait for the element to be clickable:

    buttonNoIteractable.click()
    time.sleep(10)

Alternatively search by class name. You may need to vary the wait from 10-30 seconds.

    send = browser.find_element_by_name('stacks') send.click()
scytale
  • 12,346
  • 3
  • 32
  • 46
0

The two major reason for this problem are well documented here elements not interactable reason

So 2 solutions out of which explicit wait would be better in most of the situations

Providing the solution based on python here

Implicit Wait

driver.implicitly_wait(20)

Explicit Wait in python

# impor these stataments 
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.common.by import By

# then you can run like below
userNameTextBox=wait.until(EC.visibility_of_element_located((By.NAME, "login")))
userNameTextBox.send_keys(username)

please note that all should be in CAPS after By , be it id , xpath, etc

Expected conditions for explicit wait

Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38
0

I had a problem where 2 buttons had the exact same ID. Full XPath was one way to solve the issue, but in an ever changing enviornment, full XPaths are quite often subject to change.

The first time I used Chrome.FindElement(By.Id("btnOpenLayawaysContinue") it worked, but the 2nd time it did not, because the 2nd time referred back to the first element which was in the DOM but not visible on the page.

So what I did is I built a list, and did a for-each with a try-catch inside

TheBtns = Chrome.FindElements(By.Id("btnOpenLayawaysContinue")).ToList();
foreach(IWebElement Btn in TheBtns) {
    try {
        Btn.Click();
        Thread.Sleep(1000);
        return;
    } catch {}
}
Jerven Clark
  • 1,191
  • 2
  • 13
  • 26
0

flex box size is determined by screen size. so you should put screen size to selenium driver.

In my case this works

        self.driver.set_window_position(0, 0)
        self.driver.set_window_size(1400, 900)
0

First of all I change chrome driver

  1. install pip install webdriver-manager

  2. Change services to new webdriver

     from selenium import webdriver
     from selenium.webdriver.chrome.service import Service as ChromeService
    
     service = ChromeService(ChromeDriverManager().install())  
     driver = webdriver.Chrome(service=service, options=chrome_options)
    

(Mention in comments if you need my webdriver chrome_options)

Then i use Actions to click elements. In my example, i gether facebook 'more info' buttons and clicking on this elements

more_btns = driver.find_elements(By.XPATH, '//div[@class="qi72231t" and contains(text(), "Ещё")]')

    for btn in more_btns:
        try:
            driver.implicitly_wait(10)
            ActionChains(driver).move_to_element(btn).click(button).perform()
        except:
            print('ERROR using ActionChains(driver).move_to_element(btn).click(button).perform()')

In Facebook my first iteration throw error but all next buttons works fine

White Shot
  • 115
  • 4
0

My case was a bit weird, I was trying to target the input (radio) button directly using xpath, but then I tried to target the label that contains that input (radio) and it worked...

М.Б.
  • 1,308
  • 17
  • 20
0

This code will work for every type of click

button=browser.find_element(By.XPATH, '*//span[text()="Price"]') 
time.sleep(3)
browser.execute_script("arguments[0].click();", button)

'*//span[text()="Price"]') Replace span with your required tag and Price with the text of tag

  • No, Sleeping, hoping the DOM will be ready after a set period of time, will not always work. You should wait until the element is visible, not just for a set period of time. – Ryan Shillington Aug 02 '23 at 11:56
-1

It will be better to use xpath

from selenium import webdriver
driver.get('www.example.com')
button = driver.find_element_by_xpath('xpath')
button.click()
-1

For the html code:

test.html

<button class="dismiss"  onclick="alert('hello')">
    <string for="exit">Dismiss</string>
</button>

the below python code worked for me. You can just try it.

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get("http://localhost/gp/test.html")
button = driver.find_element_by_class_name("dismiss")
button.click()
time.sleep(5)
driver.quit()
-1

I have found that using Thread.sleep(milliseconds) helps almost all the time for me. It takes time for the element to load hence it is not interactable. So i put Thread.sleep() after selecting each value. So far this has helped me avoid the error.

try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}

        Select nationalityDropdown=new Select(driver.findElement(By.id("ContentPlaceHolderMain_ddlNationality")));

        nationalityDropdown.selectByValue("Indian");

        try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}