Automating Selenium Headless mode errors

℡╲_俬逩灬. 提交于 2021-02-08 09:06:59

问题


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()

回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!