简体   繁体   中英

Automating Selenium Headless mode errors

I tried automating the below code with headful mode and it works but not in headless mode. Throws a lot of javascript errors and finally says unable to find the element.

chrome_options = Options()
chrome_options.headless = True
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
chrome_options.add_argument(
    "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
chrome_options.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome('chromedriver.exe',options=chrome_options)
driver.get('https://www.southwest.com/air/low-fare-calendar/select-dates.html?adultPassengersCount=1&currencyCode=USD&departureDate=2021-02-01&destinationAirportCode=MCO&originationAirportCode=ALB&passengerType=ADULT&returnAirportCode=&returnDate=&tripType=oneway')
time.sleep(2)
driver.find_element_by_xpath('//div[@class="flyout-trigger list-box"]').click()
time.sleep(1)
driver.find_element_by_id("Listbox_10--item-3").click()
time.sleep(1)
driver.save_screenshot("screenshot_after.png")
driver.quit()

When I went to the driver.get() it had some errors I am not 100% sure if it's bot detection which when I disabled navigator.webdriver it seemed to function correctly.

chrome_options.add_argument('--disable-blink-features=AutomationControlled')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM