How do you fix the "element not interactable" exception?
PythonPython 3.xSeleniumSelenium WebdriverWebdriverPython Problem Overview
I know this has been asked lots of times before but how do you get around the "element not interactable" exception?
I'm new to Selenium so excuse me if I get something wrong.
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.
Thanks in advance.
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")
Note: I really appreciate the help that has been given, thanks
Python Solutions
Solution 1 - Python
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
button = driver.find_element_by_class_name(u"infoDismiss")
driver.implicitly_wait(10)
ActionChains(driver).move_to_element(button).click(button).perform()
Solution 2 - Python
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()
Solution 3 - Python
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()))
Solution 4 - Python
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")
Solution 5 - Python
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 : )
Solution 6 - Python
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()
Solution 7 - Python
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);
}
}
}
Solution 8 - Python
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();}
Solution 9 - Python
> use id of the element except x_path.It will work 100%
Solution 10 - Python
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()
Solution 11 - Python
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
Solution 12 - Python
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 {}
}
Solution 13 - Python
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)
Solution 14 - Python
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()
Solution 15 - Python
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()