python - Python、Selenium 和 Chrome:无法加载页面,因为“不安全”警告
问题描述
我正在尝试通过 Python、Selenium 和 Chrome 自动化网页。代码是:
import time
from multiprocessing.dummy import Pool as ThreadPool
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = "http://some-proxy.net:8090"
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-web-security')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--enable-automation")
chrome_options.add_argument("--disable-save-password-bubble")
chrome_options.add_argument("test-type")
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("test-type=browser")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver_path = 'M:/chromedriver/chromedriver.exe'
driver = webdriver.Chrome(driver_path, chrome_options=chrome_options,desired_capabilities=capabilities)
driver.get('https://somepage.csintra.net/')
Chrome 会打开,但不会加载页面。相反,我收到以下“不安全”警告:
根据上面的代码,我尝试禁用从各种 SO 答案收集的各种 Chrome 选项,但无济于事。我也按照这里chrome_options.add_argument('--headless')
的建议尝试过,但这会导致 chromedriver.exe 中出现以下错误:
无法解析服务名称:指标
如上面的屏幕截图所示,我也无法在 Selenium 打开 Chrome 时取消阻止通知、弹出窗口和位置,因为它们被管理员阻止 - 我在工作中这样做(但无论如何,在我的 Chrome 设置中“[*].csintra.net " 被允许打开弹出窗口和通知)。
运行代码时 Python 的控制台输出:
Traceback (most recent call last):
File "<ipython-input-40-82760f33374f>", line 1, in <module>
runfile('M:/python scripts/coding_automation.py', wdir='M:/python scripts')
File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "M:/python scripts/coding_automation.py", line 35, in <module>
driver = webdriver.Chrome(driver_path, chrome_options=chrome_options,desired_capabilities=capabilities)
File "C:\Users\M299700\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\M299700\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webdriver.py", line 156, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\M299700\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webdriver.py", line 251, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\M299700\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\Users\M299700\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: unknown error: Chrome failed to start: crashed
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\Program Files (x86)\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.1.7601 SP1 x86_64)
有没有办法绕过这个?
编辑:
如上所述,在“重复”线程中发布的解决方案没有帮助。
解决方案
我觉得你必须抛出一个异常并检查它所做的某种调用。要么知道错误屏幕的 div 类型,然后用 DoesNotExist 兔子洞检查。
推荐阅读
- java - 基于json中属性的自定义序列化
- angular - canActivate 和 canDeactivate 不能一起工作
- kubernetes - 将 docker 图像作为二进制文件保存在 helm 图表中
- excel-formula - 如何在多个单元格中搜索多个单词,输出是或否?
- accessibility - AMP 如何/如何影响可访问性?
- java - AWS java SDK 升级后连接到 SQS 队列被拒绝
- sql-server - Azure 数据工作室不会重新连接到本地托管的数据库
- powershell - 从 CMD 运行时删除 PowerShell .ps1 脚本文件
- android - 如何删除Gridview之间的水平空间?
- snmp - 如何解析使用 sharpsnmplib 处理的 snmp 陷阱