selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable when clicking on an element using Selenium Python
Asked Answered
G

6

9

I understand this question has been asked but I need some solution for this error:

 Traceback (most recent call last):
 File "goeventz_automation.py", line 405, in <module>
if login(driver) is not None:
File "goeventz_automation.py", line 149, in login
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@track-element='header-login']"))).click()
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

This is the code where its getting error:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import TimeoutException
import urllib.request as request
import urllib.error as error
from PIL import Image
from selenium.webdriver.chrome.options import Options
import datetime as dt
import time
from common_file import *
from login_credentials import *

def login(driver):
global _email, _password
if waiter(driver, "//a[@track-element='header-login']") is not None:
    #login = driver.find_element_by_xpath("//a[@track-element='header-login']")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@track-element='header-login']"))).click()
    #login.click()
    if waiter(driver,"//input[@id='user_email']") is not None:
        email = driver.find_element_by_xpath("//input[@id='user_email']")
        password = driver.find_element_by_xpath("//input[@id='password']")
        email.send_keys(_email)
        password.send_keys(_password)
        driver.find_element_by_xpath("//button[@track-element='click-for-login']").click()
        return driver
    else:
        print("There was an error in selecting the email input field. It may be the page has not loaded properly.")
        return None
else:
    print("There was an error in selecting the header-login attribute on the page.")
    return None

if __name__ == '__main__':
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')

    driver = webdriver.Chrome('/usr/bin/chromium/chromedriver',chrome_options=chrome_options)
    #d.get('https://www.google.nl/')
    #driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get('https://www.goeventz.com/')
    if login(driver) is not None:
        print(create_event(driver))

I think there is some problem with Keys.ENTER, but I don't know how to solve this. I have tried every possible solution.............

Gaultheria answered 19/3, 2019 at 7:9 Comment(2)
I think it would be prudent to include your code for the function called "waiter", as it has to do with the error that you have edited the question to show. It would be nice to see all the relevant imports that you've made so we can properly rule that out for any clues or causes to your problem.Paleogeography
Make sure you dont have any iframe in your DOM, Also there is a method called isEnabled use that & make sure that the element is enabled & then try to click it.Garibold
C
10

This error message...

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

...implies that the desired element was not interactable when you tried to invoke click() on it.

A couple of facts:

  • When you initialize the Chrome browser always in maximized mode.
  • You can disable-extensions.
  • You need to disable-infobars as well.

I have used the same xpath which you have constructed and you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized");
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.goeventz.com/")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@track-element='header-login']"))).click()
    
  • Browser Snapshot:

login_page

Cryptogram answered 19/3, 2019 at 8:25 Comment(0)
P
7

copy full xpath instead of copying only xpath. It will work

Poppy answered 3/1, 2021 at 6:20 Comment(0)
T
4

Instead of using login.send_keys(Keys.ENTER) you should use selenium click() method which would work fine for you.

You can check first if the element is clickable first and then you can click on it. Like:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@track-element='header-login']"))).click()
Talmudist answered 19/3, 2019 at 7:13 Comment(7)
i still getting the same error after changing to this methodGaultheria
this code is working fine on local but its getting problem on serverGaultheria
@Gaultheria have updated my answer. Please try the updated answer.Talmudist
And you dont need to use login = driver.find_element_by_xpath("//a[@track-element='header-login']") you can just use the statement i have used in the answer.Talmudist
i have remove the code and added this one but still facing issuesGaultheria
The server on which you are trying to run the script has the same OS and same browser ?Talmudist
Well then its strange that its working on your local and not working on the server. Lets see if someone else answers on itTalmudist
P
3

Overview

It seems like you're having an XPATH problem finding the "Submit" button or your Submit button is not clickable, or your Submit button has some client side events attached to it (javascript/etc) that are required in order to effectively submit the page.

Calling the pw.submit() method in most cases should get rid of the need to wait for the submit button to become clickable and avoid any issues in locating the button in most cases. On many other websites, some of the necessary back-end processes are primed by client-side activities that are performed after the "submit" button is actually clicked (although on a side-note this is not considered best-practice because it makes the site less accessible, etc, I digress). Above all, it's important to watch your script execute and make sure that you're not getting any noticeable errors displayed on the webpage about the credentials that you're submitting.

Also, however, some websites require that you add a certain minimum amount of time between the entry of the username, password, and submitting the page in order for it to be considered a valid submitting process. I've even run in to websites that require you to use send_keys 1 at a time for usernames and passwords to avoid some anti-scraping technologies they employ. In these cases, I usually use the following between the calls:

from random import random, randint

def sleepyTime(first=5, second=10):
    # returns the value of the time slept (as float)
    # sleeps a random amount of time between the number variable in first
    # and the number variable second (in seconds)
    sleepy_time = round(random() * randint(first, second), 2)
    sleepy_time = sleepy_time if sleepy_time > first else (first + random())
    sleep(sleepy_time)
    return sleepy_time

I don't see what use you have for making the _email and _password variables global, unless they are being changed somewhere in the login function and you want that change to be precipitated out to the other scopes.

How I would try to solve it

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException, TimeoutException

TIME_TIMEOUT = 20 # Twenty-second timeout default

def eprint(*args, **kwargs):
    """ Prints an error message to the user in the console (prints to sys.stderr), passes
    all provided args and kwargs along to the function as usual. Be aware that the 'file' argument
    to print can be overridden if supplied again in kwargs.
    """
    print(*args, file=sys.stderr, **kwargs)


def login(driver):
    global _email, _password
    try:
        email = WebDriverWait(driver, TIME_TIMEOUT).until(EC.presence_of_element_located((By.XPATH, "//input[@id='user_email']")))
        pw = WebDriverWait(driver, TIME_TIMEOUT).until(EC.presence_of_element_located((By.XPATH, "//input[@id='password']"))
        pw.submit()
        # if this doesn't work try the following:
        # btn_submit = WebDriverWait(driver, TIME_TIMEOUT).until(EC.element_to_be_clickable((By.XPATH, "//button[@track-element='click-for-login']"))
        # btn_submit.click() 
        # if that doesn't work, try to add some random wait times using the 
        # sleepyTime() example from above to add some artificial waiting to your email entry, your password entry, and the attempt to submit the form.

except NoSuchElementException as ex:
    eprint(ex.msg())

except TimeoutException as toex:
    eprint(toex.msg)

if __name__ == '__main__':
    driver = webdriver.Chrome('/usr/bin/chromium/chromedriver',chrome_options=chrome_options)
    #d.get('https://www.google.nl/')
    #driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get('https://www.goeventz.com/')
    if login(driver) is not None:
        print(create_event(driver))
Paleogeography answered 19/3, 2019 at 8:34 Comment(0)
B
3

For headless chrome browser you need to provide window size as well in chrome options.For headless browser selenium unable to know what your window size.Try that and let me know.

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('window-size=1920x1480')
Barbellate answered 19/3, 2019 at 10:48 Comment(2)
This worked for me when running headless Chrome on Heroku, thanks.Chairman
@Barbellate i had it on full screen and all worked, suddenly stopped and your solution helped by setting certain window size , why is that ?Liard
C
0

I faced this error as well. Now check your browser if the element is inside the iframe. If so, use driver.find_element(By.CSS_SELECTOR, "#payment > div > div > iframe") and driver.switch_to.frame(iframe) Then you will be able to work out.

Cormorant answered 5/7, 2022 at 0:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.