无头模式下 Selenium 的超时异常

Timeout exception with Selenium in Headless mode

我通过以下方式创建驱动程序:

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)

使用后:

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)

我得到:

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: 

但没有无头模式也能正常工作

我也在尝试在vds上启动它并通过ip使用代理登录。

我可能错了,但是无头选项是 --headless,你忘记了 -- 部分。

这个错误信息...

    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

...暗示 TimeoutException 在搜索元素时引发 WebDriverWait for the visibility of the element with the following based :

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

HTML DOM is loaded .

时偶尔会观察到这种现象

您可能想构建一个更规范的 which identifies the uniquely within the DOM Tree,应该单独讨论。