Chrome-Devtools-Protocol
- 前提:chromium无头模式下和gui模式是不同的, userData无法指定, 也无法通过模拟点击alert授权。或者进入chrome://settings 通过selenium控制授权. 因为无头模式下根本就没有这些页面,userdata在无头模式下也无法指定,只能使用默认的设置!!!
- chromium headless 下perference是不起作用的, firefox 似乎可行
- headless chromium 和有gui的有不同的 profile structure. ssue 775703: Headless maintains a different profile folder structure to headful
- 解决方法: cdp方法
解决代码:
protected void grantPermission(String permission) {
ArrayList<String> permissionList = new ArrayList<>();
permissionList.add(permission);
HashMap permissionDescriptor = new HashMap();
permissionDescriptor.put("name", permission);
permissionDescriptor.put("sysex", true);
Map uaMap = new HashMap() {{
put("permission", permissionDescriptor);
put("setting", "granted");
}};
((ChromeDriver) webDriver).executeCdpCommand("Browser.setPermission",
uaMap);
}
通过cdp设置似乎是我目前看到的唯一解!
另外附上操作剪贴板的方法(前提需要开放权限)
- step1 开放权限
grantPermission("clipboard-write");
grantPermission("clipboard-read");
- step2 赋值剪贴板
protected void saveStringAsTextHtmlIntoClipboardV2(String html){
logger.info("拷贝");
String insertLine = html.replaceAll("\"", "\\\\\\\"").replaceAll("\\\n", "");
String exeStr = "function setClipDataV2(str) {\n" +
" console.log(\"Writing to clipboard\"); \n" +
" var blob = new Blob([str], {type: 'text/html'});\n" +
" var item = new ClipboardItem({'text/html': blob});\n" +
" navigator.clipboard.write([item]).then(function() {\n" +
" console.log(\"Copied to clipboard successfully!\");\n" +
" }, function(error) {\n" +
" console.log(\"unable to write to clipboard. Error:\");\n" +
" console.log(error);\n" +
" })\n" +
"}; setClipDataV2(\"" + insertLine + "\")";
((JavascriptExecutor) getWebDriver()).executeScript(exeStr);
}
以上
另外附带一些selenium的资源
镜像构建
- chrome-linux下载
- chrome-driver下载
- alpine chromedriver
- geckodriver
- chrome driver:84.0.4147.30
新的镜像构建
- 用
alpine apk
自带的chromium
代替chrome
。alpine
内自带了一套chromium
和chromedriver
跳过selenium detection
Handle Permission Pop-ups 处理授权弹出框
- Handle Permission Pop-ups
- 权限配置 declare_permissions
- cdp permission
- alpine-chromium
- alpine-chrome-driver
- List of Chromium Command Line Switches(ChromeOptions 启动参数)
- chromium headless 下perference是不起作用的, firefox 似乎可行
- headless chromium 和有gui的有不同的 profile structure. ssue 775703: Headless maintains a different profile folder structure to headful
- 无法设置上下文的解决办法: 需要重新编译
- 无头模式关闭了alert弹窗