无头模式下 Selenium 超时异常

发布于 2025-01-13 11:13:54 字数 1649 浏览 3 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那请放手 2025-01-20 11:13:54

你已经得到了答案,但我发现了这个:options.add_argument("--headless=new")
这对我有用。

You already got an answer but I found this: options.add_argument("--headless=new")
and it worked for me.

情独悲 2025-01-20 11:13:54

此错误消息...

    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

...意味着在搜索引发 WebDriverWaitTimeoutException a> 用于元素的可见性,并带有以下基于 xpath定位器策略

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

HTML DOM 在无头模式下的加载方式不同

您可能想构建一个更规范的定位器策略来标识WebElementDOM Tree 应该单独讨论。

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.

温柔少女心 2025-01-20 11:13:54

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文