Element not interactable in Selenium Chrome Headless Mode

Viewed 5593

My code is working all fine when I don't run chrome in headless mode, but in headless mode I get 'Element not interactable'.

I get error at email_box.send_keys('')

And I have set the window size, still it is not working

Code:

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time

options = Options()
options.add_argument('headless')
options.add_argument('window-size=1366x768')

with Chrome(options=options) as driver:
    driver.get('https://accounts.google.com/login')

    WebDriverWait(driver, 20).until(lambda d: d.find_element(By.TAG_NAME, 'input'))

    time.sleep(2)
    email_box = driver.find_element(By.TAG_NAME, 'input')
    time.sleep(2)
    email_box.send_keys('example@gmail.com')
4 Answers

If anyone wants another solution for this, I found this one as well. For some reason when the window isn't maximized you may have trouble clicking elements:

Add the following parameters to chromedriver in the Python environment

from selenium.webdriver.chrome.options import Options

def get_options():
    chrome_options = Options()
    chrome_options.add_argument("--window-size=1920,1080")
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument("--headless")
    return chrome_options

To send the gmail to the input tag do the following.

from selenium.webdriver.support import expected_conditions as EC

email_box=WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@type='email']")))
driver.implicitly_wait(2)
email_box.send_keys('example@gmail.com')

So I tried all the proposed solutions but nothing worked for me. In my case, I was crawling a SPA using AngularJS. The solution that I found was the following option setting for the webdriver:

options.add_argument("--window-size=1920,1080")

And after that just waiting for the element you want to click to be clickable like shown before:

elem = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, 'somexpath')))

I tried maximizing window both ways i.e. from options as well as directly from the driver instance. But both ways did not work for me.

Here is the link to the Github issue page where I found the solution: https://github.com/angular/protractor/issues/5273

Cheers

If you try to debug and print outerHTML of "email_box", it's looking for an element that is not interactable which is

Be more specific/unique with your locator. You may use //input[@type="email"] for email field

Related