My selenium code is working without headless mode but when I try to do with headless mode , it raise TimeOutException error. I am using python selenium chrome webdriver. I am testing this on my local machine at address http://127.0.0.1:5000/ I am trying to log in Instagram with selenium but it's not working in headless mode. I tries to resize the window (maximizing, manually setting the size, resize_to) but none of them working.
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("—disable-gpu")
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_driver = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver,chrome_options=chrome_options)
driver.set_window_size(1382, 744)
I am using this below helper function to wait for elements
def waiting(xpath):
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, xpath))
)
return element
My code to login in Instagram is below :
driver.get("https://www.instagram.com")
username = waiting("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input")
username.send_keys("abcdeff")
password = waiting("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input")
password.send_keys("password")
driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button").click()
I am getting this error:
Traceback (most recent call last):
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "C:\Users\madhav\Desktop\Fprojects\insta-sam\temp.py", line 47, in login
username = waiting("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input")
File "C:\Users\madhav\Desktop\Fprojects\insta-sam\temp.py", line 30, in waiting
element = WebDriverWait(driver, 20).until(
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: