I cannot get the page to load when running Selenium WebDriver in headless Chrome, even after introducing a delay and adjusting many ChromeOptions. Here’s one of many configurations I tried:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)
driver.get("https://www.example.com")
time.sleep(5)
wait = WebDriverWait(driver, 30)
try:
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "some-selector")))
except Exception as e:
print("Exception caught:", e)
When I omit the headless mode everything works as expected, but no matter what flags and timeouts I use, the page does not load. Could it be a problem with the specific pages I try to access? (I tried many)
random_person_0609 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.