I have a Selenium test case that works when headless option is False, but fails when headless option is True.
Environment:
- Mac Apple M1 Monterey Version 12.2.1
- ChromeDriver 102.0.5005.61 (0e59bcc00cc4985ce39ad31c150065f159d95ad3-refs/branch-heads/5005@{#819})
- Chrome Browser Version 102.0.5005.61 (Official Build) (arm64)
import os
import time
from os import path
from os.path import expanduser
from os.path import exists
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
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
class MSetUp:
def __init__ (self, driverName = 'chromedriver', headless=True):
if driverName == 'chromedriver':
path = expanduser("~/")
if headless:
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
else:
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
self.driver = webdriver.Chrome(path + 'chromedriver', chrome_options=chrome_options)
def setupPPMOP(
self,
redirectUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-8E9818988A127210Y",
payPalUserEmail = "mypaypal123@gmail.com",
payPalUserPassword = "rteMyPaypal123",
payPalUserCountry = 'us'):
wait = WebDriverWait(self.driver, 30)
""" Go to paypal url """
self.driver.get(redirectUrl)
""" look for consent button to accept cookie """
time.sleep(5)
cookieAcceptConcentButton = wait.until(ec.visibility_of_element_located((By.ID, "acceptAllButton")))
if cookieAcceptConcentButton != None:
cookieAcceptConcentButton.click()
""" Find email field and enter paypal user email. """
emailField = wait.until(ec.visibility_of_element_located((By.ID, "email")))
emailField.send_keys(payPalUserEmail)
""" Find next button and click """
nextButton = self.driver.find_element_by_id('btnNext')
nextButton.click()
""" Find password field and enter paypal user password. """
passwordField = wait.until(ec.visibility_of_element_located((By.ID, "password")))
passwordField.send_keys(payPalUserPassword)
""" Find login button and click """
loginButton = wait.until(ec.visibility_of_element_located((By.ID, "btnLogin")))
loginButton.click()
""" find button Agree & Continue and click """
xpath = '//*[@id="consentButton"]'
print("xpath = " + xpath)
agreementButton = wait.until(ec.element_to_be_clickable((By.XPATH, xpath)))
The last step fails with the following exception in headless mode.
> raise TimeoutException(message, screen, stacktrace)
E selenium.common.exceptions.TimeoutException: Message:
E Stacktrace:
E 0 chromedriver 0x0000000103161170 chromedriver + 4477296
E 1 chromedriver 0x00000001030f8c18 chromedriver + 4049944
E 2 chromedriver 0x0000000102d4ad40 chromedriver + 191808
E 3 chromedriver 0x0000000102d79b28 chromedriver + 383784
E 4 chromedriver 0x0000000102da285c chromedriver + 551004
E 5 chromedriver 0x0000000102d6ef58 chromedriver + 339800
E 6 chromedriver 0x0000000103136bd4 chromedriver + 4303828
E 7 chromedriver 0x000000010313b094 chromedriver + 4321428
E 8 chromedriver 0x000000010313f600 chromedriver + 4339200
E 9 chromedriver 0x000000010313baf4 chromedriver + 4324084
E 10 chromedriver 0x000000010311a52c chromedriver + 4187436
E 11 chromedriver 0x0000000103153488 chromedriver + 4420744
E 12 chromedriver 0x00000001031535ec chromedriver + 4421100
E 13 chromedriver 0x000000010316792c chromedriver + 4503852
E 14 libsystem_pthread.dylib 0x00000001c4f09240 _pthread_start + 148
E 15 libsystem_pthread.dylib 0x00000001c4f04024 thread_start + 8