无头模式下 Selenium 超时异常
我通过以下方式创建驱动程序:
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 使用代理登录。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你已经得到了答案,但我发现了这个:
options.add_argument("--headless=new")
这对我有用。
You already got an answer but I found this:
options.add_argument("--headless=new")
and it worked for me.
此错误消息...
...意味着在搜索引发 WebDriverWaitTimeoutException a> 用于元素的可见性,并带有以下基于 xpath 的定位器策略 :
当 HTML DOM 在无头模式下的加载方式不同。
您可能想构建一个更规范的定位器策略来标识WebElement 在 DOM Tree 应该单独讨论。
This error 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:
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.
我可能是错的,但是无头选项是 --headless,你忘记了 -- 部分。
I might be wrong, but the headless option is with --headless, you forgot the -- part.