Home > other >  Timeout exception with Selenium in Headless mode
Timeout exception with Selenium in Headless mode

Time:03-12

I create driver by:

options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('headless')
options.add_argument('disable-infobars')
options.add_argument('--remote-debugging-port=9222')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--disable-notifications")
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument('--proxy-server=%s' % proxy)
driver = webdriver.Chrome(executable_path='down/chromedriver.exe',chrome_options=options)

After using:

driver.get('https://myip.ru/')
r=WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="ipcontent"]/table/tbody/tr[2]/td'))).text
print(r)

I get:

Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "twitter.py", line 220, in main
    r=WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="ipcontent"]/table/tbody/tr[2]/td'))).text
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

But it works fine without headless mode

I am also trying to start it on vds and use proxy login by ip.

CodePudding user response:

I might be wrong, but the headless option is with --headless, you forgot the -- part.

CodePudding user response:

This error message...

    r=WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="ipcontent"]/table/tbody/tr[2]/td'))).text
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message

...implies that TimeoutException was raised while searching for the element inducing WebDriverWait for the visibility of the element with the following xpath based locator strategy:

//*[@id="ipcontent"]/table/tbody/tr[2]/td

This phenomenon can be occassionally observed when the HTML DOM is loaded differently in headless mode.

You may like to construct a more canonical locator strategy which identifies the WebElement uniquely within the DOM Tree and should be a seperate discussion all together.

  • Related