尝试让Selenium Testing与Chrome Headless一起使用(我们目前正在使用远程selenium web驱动程序,它工作正常,但速度很慢)。 ChromeDriver实例化为:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("/usr/bin/google-chrome");
chromeOptions.addArguments("--headless", "--window-size=1920x1080");
chromeOptions.setHeadless(true);
Map<String, Object> prefs = new HashMap<>();
prefs.put("credentials_enable_service", false);
prefs.put("password_manager_enabled", false);
chromeOptions.setExperimentalOption("prefs", prefs);
System.setProperty("webdriver.chrome.verboseLogging", "true");
dr = new ChromeDriver(chromeOptions);
(我已经尝试过它只使用setHeadless,并且只使用--headless作为参数,我认为我应该双重确定所以代码目前包括两者)。日志显示:
Starting ChromeDriver (v2.9.248304) on port 1848
[0.705][INFO]: COMMAND InitSession {
"capabilities": {
"desiredCapabilities": {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [ "--headless", "--disable-gpu", "--window-size=1920x1080" ],
"binary": "/usr/bin/google-chrome",
"extensions": [ ],
"prefs": {
"credentials_enable_service": false,
"password_manager_enabled": false
}
}
},
"firstMatch": [ {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [ "--headless", "--disable-gpu", "--window-size=1920x1080" ],
"binary": "/usr/bin/google-chrome",
"extensions": [ ],
"prefs": {
"credentials_enable_service": false,
"password_manager_enabled": false
}
}
} ]
},
"desiredCapabilities": {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [ "--headless", "--disable-gpu", "--window-size=1920x1080" ],
"binary": "/usr/bin/google-chrome",
"extensions": [ ],
"prefs": {
"credentials_enable_service": false,
"password_manager_enabled": false
}
}
}
}
[0.708][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
}
}
[0.708][INFO]: Populating Local State file: {
"background_mode": {
"enabled": false
},
"ssl": {
"rev_checking": {
"enabled": false
}
}
}
[0.712][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.ZsjTh0/internal --logging-level=1 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12771 --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.lVGXx8 data:,
[0.714][DEBUG]: DevTools request: http://127.0.0.1:12771/json/version
[0.732][WARNING]: PAC support disabled because there is no system implementation
[1.457][DEBUG]: DevTools request failed
[1.507][DEBUG]: DevTools request: http://127.0.0.1:12771/json/version
[1.514][DEBUG]: DevTools request failed
[1.564][DEBUG]: DevTools request: http://127.0.0.1:12771/json/version
[1.569][DEBUG]: DevTools request failed
[1.620][DEBUG]: DevTools request: http://127.0.0.1:12771/json/version
[1.624][DEBUG]: DevTools request failed
[1.674][DEBUG]: DevTools request: http://127.0.0.1:12771/json/version
[1.678][DEBUG]: DevTools request failed
[1.728][DEBUG]: DevTools request: http://127.0.0.1:12771/json/version
[1.732][DEBUG]: DevTools request failed
[1.782][DEBUG]: DevTools request: http://127.0.0.1:12771/json/version
[1.786][DEBUG]: DevTools request failed
(google-chrome:1887): Gtk-WARNING **: cannot open display:
然后又多了一点“DevTools请求:http://127.0.0.1:12771/json/version,DevTools请求失败”行(数百和数百)。
我尝试过使用Selenium 3.7.1,然后尝试更新到Selenium 3.12.0,输出没有变化。
Chrome已安装(版本66.0.3359.181),与ChromeDriver(v2.9.248304)一样,整个过程都在Centos 7上,没有Window Manager。虽然ChromeDriver似乎是启动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.ZsjTh0/internal --logging-level=1 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12771 --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.lVGXx8 data:,
(google-chrome:3346): Gtk-WARNING **: cannot open display:
如果我添加 - 无线,它会成功。
$ /opt/google/chrome/google-chrome --headless --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.ZsjTh0/internal --logging-level=1 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12771 --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.lVGXx8 data:,
[0529/154351.972190:ERROR:gpu_process_transport_factory.cc(1007)] Lost UI shared context.
DevTools listening on ws://127.0.0.1:12771/devtools/browser/a008b11c-d1c3-4c56-ba28-7f221eed4507
Fontconfig warning: "/etc/fonts/fonts.conf", line 146: blank doesn't take any effect anymore. please remove it from your fonts.conf
此设置在我的Windows框中正常工作(但我的Windows框上的启动Chrome请求确实包含--headless参数)。我不确定为什么ChromeDriver在启动时似乎没有将任何chromeOptions(--headless, - disable-gpu或--window = 1920x1024)传递给chrome进程。
任何帮助将不胜感激......
答案 0 :(得分:0)
您的代码块几乎是完美的。有几件事:
如果查看setHeadless()
的源代码,--headless
标志已经实现。因此明确设置addArguments("--headless")
将是开销。你可以删除参数。
def set_headless(self, headless=True):
"""
Sets the headless argument
Args:
headless: boolean value indicating to set the headless option
"""
args = {'--headless', '--disable-gpu'}
if headless:
self._arguments.extend(args)
else:
self._arguments = list(set(self._arguments) - args)
但是,您使用的二进制版本中的主要问题是不兼容,如下所示:
chromedriver=2.9的发行说明明确提到以下内容:
支持 Chrome v31-34
您正在使用 chrome = 66.0
ChromeDriver v2.38的发行说明明确提到以下内容:
支持 Chrome v65-67
因此 ChromeDriver v2.9 与 Chrome浏览器 v66.0之间存在明显的不匹配
@Test
。