问题
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¤cyCode=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()
回答1:
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')
来源:https://stackoverflow.com/questions/65928302/automating-selenium-headless-mode-errors