RCarmody
RCarmody

Reputation: 720

Selenium Send Keys Timeout Headless

I have the following code to log-in to my grocery store's website, and it works perfectly when in the foreground but whenever I use the options.add_argument('headless'), it fails to find the username element. Can you send_keys and navigate websites in the background?

Error:

    element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="logonId"]'))).send_keys('rcarmody09@yahoo.com')
  File "C:\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
import time
import selenium
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from pathlib import Path
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import InvalidArgumentException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import os
import re
import shutil
import pandas as pd
import pyautogui as gui

options = selenium.webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(driver2, options=options)

driver.get("https://nourish.schnucks.com/web-ext/user/login?redirectUrl=https:%2F%2Fnourish.schnucks.com%2F")
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="logonId"]'))).send_keys('email@yahoo.com')
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="password"]'))).send_keys('xxxxx')
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="loginForm"]/div/div[3]/button'))).click()

Upvotes: 1

Views: 1026

Answers (1)

KunduK
KunduK

Reputation: 33384

For headless browser you have to set the window size to fire on event. Because headless browser can't recognise where to click without window size.

options = selenium.webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1920x1080');

Upvotes: 2

Related Questions