问题
I'm using the selenium library in python to open google chrome and visit google.com automatically, this is how my script looks like at the moment
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/")
My browser tab never displays google.com and it just hangs there This is the picture of how my browser looks like when I run the script
My version of chromedriver is: ChromeDriver 2.39 My Google Chrome version is : Google Chrome 67.0
After doing Ctrl+c, i get this output
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
Any help as to why --enable-automation does not work would be greatly appreciated!
回答1:
You are missing full path of chromedriver including .exe. Use something like this or fully qualified of chromedriver
webdriver.Chrome(executable_path='drivers\chromedriver.exe')
回答2:
I modified your script in this way and was able to execute it successfully
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/")
Also, you need to use add-argument
and not add_options
for the --enable-automation
flag
来源:https://stackoverflow.com/questions/50667509/chrome-is-being-controlled-by-automated-test-software-not-secure-data-python