java - 如何将我自己的证书(.pem 文件)添加到我的 selenium chromedriver?
问题描述
我有一个 docker-compose 文件:
version: '3'
services:
selenium-3-chrome:
image: selenium/standalone-chrome-debug:3.14.0
restart: always
environment:
TZ: Europe/Budapest
SCREEN_WIDTH: 1920
SCREEN_HEIGHT: 1080
JAVA_OPTS: -Xmx128m
ports:
- 4444:4444
- 5900:5900
volumes:
- /dev/shm:/dev/shm
我的java代码:
ChromeOptions chromeOptions = util.chromeOptions(scenario);
chromeOptions.setHeadless(true);//default-bol is az
chromeOptions.addArguments("--window-size=1366,768");
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
initProperies();
if(config == null) {
readConfiguration();
try {
host = System.getProperty(HOSTNAME);
} catch(Exception e) {
//...
}
}
// itt állítom rá a docker-es chromedriverre:
WebDriver driver = new RemoteWebDriver(new URL(config.getConnection().getWebDriverUrl()), capability);
driver.manage().timeouts().pageLoadTimeout(90000, TimeUnit.MILLISECONDS);
driver.get(host+"/");
如何将自己的证书添加到功能或 chromeOptions?“/home/../rootca/lbsca.pem”-> 在 chrome 浏览器中,它位于“权威”证书中,而不是“您的证书”中。或者我可以将我的 .pem 添加到 docker 的独立 Chrome 中?我希望,我理解..对不起我的英语
解决方案
PhantomJSDriver 可用于执行此功能。
DesiredCapabilities cap = DesiredCapabilities.chrome();
ImmutableMap<String, String> commandLineArguments = ImmutableMap.<String,
String>builder()
.put("web-security", "false")
.put("ssl-protocol", "any")
.put("ignore-ssl-errors", "true")
.put("webdriver-loglevel", "DEBUG")
.put("ssl-client-certificate-file", certificatePath)
.put("ssl-client-key-passphrase", certificatePassword)
.build();
String[] params = commandLineArguments.entrySet().stream()
.map(e -> String.format("--%s=%s", e.getKey(), e.getValue()))
.collect(Collectors.toList())
.toArray(new String[0]);
cap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, params);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new PhantomJSDriver(cap);
driver.get(Url);
如果证书不是必须的,这意味着您可以通过接受不安全的证书来跳过证书控制,您可以使用以下功能跳过证书控制:
Chrome 和 Internet Explorer
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capability.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true)
火狐
首先,您需要创建一个配置文件,比如说exampleProfile
.
然后,使用以下代码在您的脚本中访问该配置文件;
ProfilesIni profIni = new ProfilesIni();
FirefoxProfile exampleProfile = profIni.getProfile("exampleProfile");
之后,您需要设置AcceptUntrustedCertificates
&AssumeUntrustedCertificateIssuer
属性。
exampleProfile.setAcceptUntrustedCertificates(true);
exampleProfile.setAssumeUntrustedCertificateIssuer(false);
最后,您可以使用以下代码初始化驱动程序;
WebDriver driver = new FirefoxDriver(exampleProfile);
推荐阅读
- java - 如何获得 Java2D 的假定屏幕分辨率?
- javascript - Angular 6 Typescript 从数组中提取元素并进行比较
- encryption - 如何在C中解密虚拟内存中的文件
- ios - UICollectionReusableView 中的 UIBUtton 操作
- php - Typo3 8.7.19 扩展迁移。错误:无法分析类
- mysql - 通过python脚本执行mysql查询时出错
- java - 带有条件的 SQLITE 随机值/行
- scala - 使用 play framework 管理外部 api 调用的缓存解决方案
- react-native - React-Native 中的线性渐变背景颜色
- gherkin - 在 Cucumber-Gherkins 集成测试的特征文件中并行运行示例