以下是我在Ubuntu 16.04上运行的代码片段。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
path_to_chromedriver = '/usr/local/bin/chromedriver' # change path as needed
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('--disable-extensions')
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--no-sandbox')
service_log_path = "{}/chromedriver.log".format('/tmp/local/')
service_args = ['--verbose']
driver = webdriver.Chrome(executable_path = path_to_chromedriver,chrome_options=options,
service_args=service_args,
service_log_path=service_log_path)
无论我放置什么标志,我总是在chromedriver.log文件中得到以下错误。
[1.001][INFO]: COMMAND InitSession {
"capabilities": {
"alwaysMatch": {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [ "--headless", "--no-sandbox" ],
"extensions": [ ]
},
"platformName": "any"
},
"firstMatch": [ {
} ]
},
"desiredCapabilities": {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [ "--headless", "--no-sandbox" ],
"extensions": [ ]
},
"platform": "ANY",
"version": ""
}
}
[1.001][INFO]: Populating Preferences file: {
"alternate_error_pages": {
"enabled": false
},
"autofill": {
"enabled": false
},
"browser": {
"check_default_browser": false
},
"distribution": {
"import_bookmarks": false,
"import_history": false,
"import_search_engine": false,
"make_chrome_default_for_user": false,
"show_welcome_page": false,
"skip_first_run_ui": true
},
"dns_prefetching": {
"enabled": false
},
"profile": {
"content_settings": {
"pattern_pairs": {
"https://*,*": {
"media-stream": {
"audio": "Default",
"video": "Default"
}
}
}
},
"default_content_settings": {
"geolocation": 1,
"mouselock": 1,
"notifications": 1,
"popups": 1,
"ppapi-broker": 1
},
"password_manager_enabled": false
},
"safebrowsing": {
"enabled": false
},
"search": {
"suggest_enabled": false
},
"translate": {
"enabled": false
}
}
[1.001][INFO]: Populating Local State file: {
"background_mode": {
"enabled": false
},
"ssl": {
"rev_checking": {
"enabled": false
}
}
}
[1.001][INFO]: Launching chrome: /opt/google/chrome/google-chrome --disable-background-networking --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --disable-hang-monitor --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-logging --full-memory-crash-report --ignore-certificate-errors --load-extension=/tmp/.com.google.Chrome.np7aOC/internal --logging-level=1 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12652 --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.iUR4hY data:,
[1.001][DEBUG]: DevTools request: http://127.0.0.1:12652/json/version
[1.002][WARNING]: PAC support disabled because there is no system implementation
[927:927:0316/063553.173763:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
[1.037][DEBUG]: DevTools request failed
[1.087][DEBUG]: DevTools request: http://127.0.0.1:12710/json/version
[1.087][DEBUG]: DevTools request failed
[1.138][DEBUG]: DevTools request: http://127.0.0.1:12710/json/version
................................................................
................................................................
[61.031][INFO]: RESPONSE InitSession unknown error: Chrome failed to start: exited abnormally
[61.031][DEBUG]: Log type 'driver' lost 0 entries on destruction
[61.031][DEBUG]: Log type 'browser' lost 0 entries on destruction
我也尝试从非root用户运行它,但是,我仍然得到同样的错误。
我只能通过SSH连接到这台机器。我试过,如果在屏幕上运行命令可以帮助但没有用,也没有使用crontab。
任何指导都会非常有用。
更新1:
以下是重启后的chromedriver日志:
[1.003][INFO]: COMMAND InitSession {
"capabilities": {
"alwaysMatch": {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [ "start-maximized", "--disable-extensions", "--headless", "--disable-gpu", "--no-sandbox" ],
"extensions": [ ]
},
"platformName": "any"
},
"firstMatch": [ {
} ]
},
"desiredCapabilities": {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [ "start-maximized", "--disable-extensions", "--headless", "--disable-gpu", "--no-sandbox" ],
"extensions": [ ]
},
"platform": "ANY",
"version": ""
}
}
[1.003][INFO]: Populating Preferences file: {
"alternate_error_pages": {
"enabled": false
},
"autofill": {
"enabled": false
},
"browser": {
"check_default_browser": false
},
"distribution": {
"import_bookmarks": false,
"import_history": false,
"import_search_engine": false,
"make_chrome_default_for_user": false,
"show_welcome_page": false,
"skip_first_run_ui": true
},
"dns_prefetching": {
"enabled": false
},
"profile": {
"content_settings": {
"pattern_pairs": {
"https://*,*": {
"media-stream": {
"audio": "Default",
"video": "Default"
}
}
}
},
"default_content_settings": {
"geolocation": 1,
"mouselock": 1,
"notifications": 1,
"popups": 1,
"ppapi-broker": 1
},
"password_manager_enabled": false
},
"safebrowsing": {
"enabled": false
},
"search": {
"suggest_enabled": false
},
"translate": {
"enabled": false
}
}
[1.003][INFO]: Populating Local State file: {
"background_mode": {
"enabled": false
},
"ssl": {
"rev_checking": {
"enabled": false
}
}
}
[1.004][INFO]: Launching chrome: /opt/google/chrome/google-chrome --disable-background-networking --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --disable-hang-monitor --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-logging --full-memory-crash-report --ignore-certificate-errors --load-extension=/tmp/.com.google.Chrome.wpOJGk/internal --logging-level=1 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12725 --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.XeSkzL data:,
[1.004][DEBUG]: DevTools request: http://127.0.0.1:12725/json/version
[1.008][WARNING]: PAC support disabled because there is no system implementation
[1752:1752:0321/084842.042837:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
[1.051][DEBUG]: DevTools request failed
[1.102][DEBUG]: DevTools request: http://127.0.0.1:12725/json/version
[1.102][DEBUG]: DevTools request failed
[1.152][DEBUG]: DevTools request: http://127.0.0.1:12725/json/version
[1.152][DEBUG]: DevTools request failed
[1.203][DEBUG]: DevTools request: http://127.0.0.1:12725/json/version
答案 0 :(得分:0)
错误说明了一切:
[455:455:0316/061123.770013:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
[61.031][INFO]: RESPONSE InitSession unknown error: Chrome failed to start: exited abnormally
错误堆栈跟踪清楚地提到您在没有 - no-sandbox 参数的情况下以 root 用户身份执行该程序。
请按照以下步骤操作:
使用以下代码块:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
path_to_chromedriver = '/path/to/chromedriver'
options = Options()
options.add_argument("start-maximized")
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--disable-extensions")
options.add_argument("--no-sandbox")
service_args = ['--verbose']
service_log_path = '/tmp/local/chromedriver.log'
driver = webdriver.Chrome(executable_path='/path/to/chromedriver', chrome_options=options, service_args=service_args, service_log_path=service_log_path)
driver.get('http://google.com/')
因为您仍然看到错误:
[927:927:0316/063553.173763:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
请尝试以下步骤:
@Test
。根据强制性Requirements和#1341确保以下内容:
/usr/local/bin/
/usr/bin/google-chrome
关于[1.008][WARNING]: PAC support disabled
,根据讨论PAC support disabled because there is no system implementation
根据讨论How to make firefox headless programatically in Selenium with python?中的更新部分,您可以尝试使用 set_headless(headless = True)方法作为替代方法,如下所示:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.set_headless(headless=True)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("http://google.com/")
print ("Headless Chrome Initialized")
driver.quit()