Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element not interactable in Selenium Chrome Headless Mode

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('[email protected]')
like image 257
Mubbashir Ali Avatar asked Sep 07 '20 20:09

Mubbashir Ali


3 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
like image 95
MarinaF Avatar answered Nov 12 '22 04:11

MarinaF


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('[email protected]')
like image 22
Arundeep Chohan Avatar answered Nov 12 '22 03:11

Arundeep Chohan


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

like image 1
sanyog yadav Avatar answered Nov 12 '22 03:11

sanyog yadav