Chrome由自动化测试软件“不安全”数据控制,即Python

时间:2018-06-03 14:36:30

标签: python google-chrome selenium

我正在使用python中的selenium库打开谷歌浏览器并自动访问google.com,这就是我的脚本目前的样子

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chromedriver = "/usr/bin/chromedriver"

os.environ["webdriver.chrome.driver"] = chromedriver
chrome_options = webdriver.ChromeOptions()
chrome_options.accept_untrusted_certs = True
chrome_options.assume_untrusted_cert_issuer = True
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--allow-http-screen-capture")
chrome_options.add_argument("--disable-impl-side-painting")
chrome_options.add_argument("--disable-setuid-sandbox")
chrome_options.add_argument("--disable-seccomp-filter-sandbox")
chrome_options.add_options("--enable-automation")
chrome_options.add_options("--disable-infobar")
driver = webdriver.Chrome(chromedriver, chrome_options=chrome_options)

driver.get("http://google.com/")

我的浏览器标签永远不会显示google.com,只会挂起This is the picture of how my browser looks like when I run the script enter image description here

我的chromedriver版本是:ChromeDriver 2.39 我的Google Chrome版本为:Google Chrome 67.0

执行Ctrl + c后,我得到此输出

Traceback (most recent call last):
File "auto-run.py", line 16, in <module>
driver = webdriver.Chrome(chrome_path, chrome_options=chrome_options)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 156, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 245, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 472, in execute
return self._request(command_info[0], url, body=data)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 496, in _request
resp = self._conn.getresponse()
File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
response.begin()
File "/usr/lib/python3.6/http/client.py", line 297, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.6/http/client.py", line 258, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib/python3.6/socket.py", line 586, in readinto
return self._sock.recv_into(b)
KeyboardInterrupt

任何帮助,为什么 - 启动自动化不起作用将非常感谢!

2 个答案:

答案 0 :(得分:0)

您缺少chromedriver的完整路径,包括 .exe 。使用类似这样的东西或完全合格的chromedriver

part_key

答案 1 :(得分:0)

我以这种方式修改了你的脚本并且能够成功执行它

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_path  = '/Users/zac/Desktop/chromedriver_mac64/chromedriver'
#this would be the path to the chromedriver exe on your system

chrome_options = webdriver.ChromeOptions()
chrome_options.accept_untrusted_certs = True
chrome_options.assume_untrusted_cert_issuer = True
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--allow-http-screen-capture")
chrome_options.add_argument("--disable-impl-side-painting")
chrome_options.add_argument("--disable-setuid-sandbox")
chrome_options.add_argument("--disable-seccomp-filter-sandbox")
driver = webdriver.Chrome(chrome_path, chrome_options=chrome_options)

driver.get("http://google.com/")

此外,您需要使用add-argument而非add_options作为--enable-automation标记