解决办法:直接把sandbox禁用了。
需要导入:
from selenium.webdriver.chrome.options import Options
chrome_options.add_argument(’–no-sandbox’)# “–no - sandbox”参数是让Chrome在root权限下跑
chrome_options.add_argument(’–disable-dev-shm-usage’)
chrome_options.add_argument(’–headless’) # “–headless”参数是不用打开图形界面(#浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败)
options = Options()
options.add_argument(’–headless’)
options.add_argument(’–no-sandbox’)
options.add_argument(’–disable-dev-shm-usage’)
driver = webdriver.Chrome(executable_path="/root/chromedriver",
chrome_options=options)
实际应用中的代码是:
chrome_options = Options()
chrome_options.add_argument(’–no-sandbox’)
chrome_options.add_argument(’–disable-dev-shm-usage’)
chrome_options.add_argument(’–headless’)
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.url = “http://192.0.4.79/#/login”
self.driver.implicitly_wait(5)
报错信息如下:
ft1.1: Traceback (most recent call last):
File “test_standard_debug.py”, line 11, in setUp
self.driver = webdriver.Chrome()
File “/home/hanxia/venv/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py”, line 81, in init
desired_capabilities=desired_capabilities)
File “/home/hanxia/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”, line 157, in init
self.start_session(capabilities, browser_profile)
File “/home/hanxia/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”, line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File “/home/hanxia/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”, line 321, in execute
self.error_handler.check_response(response)
File “/home/hanxia/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py”, line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn’t exist)
(The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Linux 4.15.0-47-generic x86_64)
Traceback (most recent call last):
File “test_standard_debug.py”, line 11, in setUp
self.driver = webdriver.Chrome()
File “/home/hanxia/venv/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py”, line 81, in init
desired_capabilities=desired_capabilities)
File “/home/hanxia/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”, line 157, in init
self.start_session(capabilities, browser_profile)
File “/home/hanxia/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”, line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File “/home/hanxia/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”, line 321, in execute
self.error_handler.check_response(response)
File “/home/hanxia/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py”, line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn’t exist)
(The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Linux 4.15.0-47-generic x86_64)