如何修复Selenium错误"ERROR: The requested URL could not be retrieved"?



在gitlab CI的docker中,我正在运行一个非常短的python selenium测试代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()

chrome_options.add_argument("--headless")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--start-maximized')
driver = webdriver.Chrome(options=chrome_options)
driver.quit()

但是它失败了,错误是

selenium.common.exceptions.WebDriverException: Message: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta type="copyright" content="Copyright (C) 1996-2016 The Squid Software Foundation and contributors">
<meta http-equiv="Content-Type" CONTENT="text/html; charset=utf-8">
<title>ERROR: The requested URL could not be retrieved</title>
<style type="text/css"><!-- 
/*
* Copyright (C) 1996-2016 The Squid Software Foundation and contributors
....

使用包:

  • 硒4.0.0
  • chrome浏览器92.0.4515.107
  • chromedriver 92.0.4515.43

我能试着解决这个问题吗?

提示

尝试获取selenium驱动程序时出现错误!!

这意味着您无法访问目标URL ("https://www.google.com")。检查你的网络是否需要任何代理(在Python中使用代理运行Selenium Webdriver)或VPN运行。

问题是selenium 4.0.0似乎有一个关于代理的错误。不要用4.0.0,用3.141.0。

可能的相关罚单:https://github.com/SeleniumHQ/selenium/issues/9925

相关内容

最新更新