I’m developing a Python script to log into a website and check if there are available appointment slots. It works perfectly when I run the browser normally, but in headless mode, I’m only getting ‘Unavailable’ in the page source instead of the expected content.
Here’s an example of my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
# Website configuration
login_url = 'https://mywebsite.com/login'
username = '[email protected]'
password = 'your_secure_password'
expected_message = "No appointments available."
# Headless Chrome settings
chrome_options = Options()
chrome_options.add_argument("--headless")
# Launch the browser
driver = webdriver.Chrome(options=chrome_options)
driver.implicitly_wait(5)
# Go to the login page
driver.get(login_url)
# Find and fill in the login fields
email_field = driver.find_element('name', 'Email')
password_field = driver.find_element('name', 'Password')
email_field.send_keys(username)
password_field.send_keys(password)
# Log in
password_field.send_keys(Keys.RETURN)
# Wait for the page to load
time.sleep(5)
# Verify the content
if expected_message in driver.page_source:
print('No availability')
else:
print('Appointments are available!')
driver.quit()
When I run the code normally, I can see the expected content after logging in. However, in headless mode, the only text returned is ‘Unavailable’.
I have tried various user agent strings and added common flags like no-sandbox and disable-gpu. Additionally, I’ve explored different waiting methods, yet the issue persists. Is it possible that the website is blocking headless browsers? What alternatives can I explore to resolve this?