如何解决Selenium WebDriver Java Chrome - 如何选择现有配置文件并自动选择个人证书?
我正在尝试创建一个 Java Selenium WebDriver 脚本来设置 Chrome 和 Firefox 浏览器。 Firefox 非常容易和成功。但是,我在设置 Chrome 代码部分以执行相同操作时遇到了 2 个问题。我在 Linux 环境中工作。
- 我无法通过代码选择正确的 Chrome 配置文件。它一直将“默认”一词放在我的个人资料路径的末尾,因此不会选择预制的个人资料。
- 我不知道如何让 Chrome 自动选择导入的证书。在 Firefox 中,我创建了单独的配置文件,并且只导入了我希望它选择的一个 PKI 证书......很简单!我似乎无法用 Chrome 来做到这一点。我可以制作个人资料。但是,每个配置文件都会看到我导入的每个 PKI 证书。 “不好玩!!!”
以下是我在 StackOverlow 的帮助下自行研究和完成的。
- 我创建了 6 个不同的 Chrome 配置文件。用户 1、用户 2、用户 3、用户 4、用户 5 和用户 6 并为他们每个人提供了自己的图标。
- 我可以通过在命令行中使用 Chrome://version 来查找配置文件位置。我的个人资料位于:/home/user/.config/google-chrome/Profile 1。其他人按预期位于个人资料 2-6。
- 我已将自己的证书和所有 5 个测试证书导入到 Chrome 浏览器中隐私和安全 > 安全 > 管理证书 >“您的证书”下的“您的证书”中。不幸的是,每个配置文件都会在“您的证书”选项卡中看到所有导入的证书???在 Firefox 中,我只能看到该配置文件的一个导入证书???
这是我编写的 Invokebrowser 类,它可以完美地处理 Firefox,但不适用于 Chrome。
package aTestNG;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;
public class Invokebrowser {
private WebDriver driver;
// The following code determines which browser to set up (e.g.,Firefox or
// Chrome). This code works!!!
public WebDriver setbrowserBasicsByUser(String browserType,String userSid) {
if (browserType.equalsIgnoreCase("FireFox")) {
driver = setFireFoxbrowser(userSid);
} else if (browserType.equalsIgnoreCase("Chrome")) {
driver = setChromebrowser(userSid);
}
return driver;
}
// This sets up the Firefox browser settings.
// This code works!!!
private WebDriver setFireFoxbrowser(String userSid) {
try {
System.out.println("Starting Firefox Driver Setup.");
// This is the location of my appropriate driver.
System.setProperty("webdriver.gecko.driver","/home/user/SwDev/browserDrivers/geckodriver");
// This code lets me pick my desired profile and PKI test certificate for the test.
ProfilesIni profIni = new ProfilesIni();
FirefoxProfile ffProfile = profIni.getProfile(userSid);
// This code gives me my desired browser preferences.
ffProfile.setPreference("security.default_personal_cert","Select Automatically");
ffProfile.setPreference("browser.startup.homepage","https://home.page.some.site.com/");
ffProfile.setPreference("startup.homepage override url","https: //home.page.some.other.site.com/");
ffProfile.setAcceptUntrustedCertificates(false);
ffProfile.setAssumeUntrustedCertificateIssuer(false);
// This code puts the Firefox profile,options,and preferences together.
FirefoxOptions ffOpts = new FirefoxOptions();
ffOpts.setProfile(ffProfile);
// This code builds the desired Firefox driver and returns it to the calling
// method.
driver = new FirefoxDriver(ffOpts);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS);
driver.manage().window().maximize();
System.out.println("Firefox Driver Setup complete.");
} catch (Exception e) {
e.printstacktrace();
}
return driver;
}
// This sets up the Chrome browser settings.
// Todo This code does not works!!! I want it to do the same things as the Firefox code above.
private WebDriver setChromebrowser(String userSid) {
try {
System.out.println("Starting Chrome Driver Setup.");
// Point to the proper Chrome driver location.
System.setProperty("webdriver.chrome.driver","/nome/user/SwDev/browserDrivers/chromedriver");
// Make a HashMap for locating the Chrome Profile based on userSid.
HashMap<String,String> chromeProfileLocation = new HashMap<String,String>();
chromeProfileLocation.put("userl","/home/user/.config/google-chrome/Profile 1");
chromeProfileLocation.put("user2","/home/user/.config/google-chrome/Profile 2");
chromeProfileLocation.put("user3","/home/user/.config/google-chrome/Profile 3");
chromeProfileLocation.put("user4","/home/user/.config/google-chrome/Profile 4");
chromeProfileLocation.put("user5","/home/user/.config/google-chrome/Profile 5");
chromeProfileLocation.put("user6","/home/user/.config/google-chrome/Profile 6");
String chromeProfilePath = chromeProfileLocation.get(userSid);
// Now use the chromeProfileLocation and the chromeProfilePath in the Chrome Options function.
// Create ChromeOptions to execute start-up commands to be executed.
ChromeOptions opts = new ChromeOptions();
opts.addArguments("--no-default-—browser-check");
opts.addArguments("--ignore-certificate-errors");
opts.addArguments("--user-data-dir=" + chromeProfilePath);
opts.addArguments("--login-profile");
// Now create the driver with all the desired Chrome Options needed.
driver = new ChromeDriver(opts);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.manage().deleteallCookies();
driver.manage().window().maximize();
System.out.println("Chrome Driver Setup complete.");
} catch (Exception e) {
e.printstacktrace();
}
return driver;
}
}
这是我的问题。当此代码运行时,我没有得到正确的 轮廓。它会在其中创建一个新目录并添加单词 “默认”到配置文件位置。因此,我不明白 我为测试构建的“真实配置文件”。
谁能告诉我我遗漏了什么或误解了什么?
- 首先,这是我在编写脚本时获得的配置文件信息 运行。这是从此代码创建的配置文件。请 看看最后一行写着“配置文件路径”。它在推动我 坚果!!!!
- 二、如何让Chrome自动选择PKI 一旦我获得证书以选择正确的配置文件? Firefox 有一个 在浏览器中自动选择个人证书的选项 当您设置个人资料时。
这是我在使用 chrome://version 命令时得到的信息。 谷歌浏览器 89.0.4389.90(官方版本)(64 位)
修订版 62eb262cdaae9ef819aadd778193781455ec7a49-refs/branch-heads/4389@{#1534}
操作系统 Linux JavaScript V8 8.9.255.20 用户代理 Mozilla/5.0 (X11; Linux x86 64) AppleWebKit/537.36 (KHTML,如 Gecko)Chrome/89.0.4389.90 Safari/537.36 命令行 /usr/bin/google-chrome --disable-background-networking --disable-client-side-phishing-detection --disable-default--apps --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-automation --enable-blink-features=ShadowDOMVO --enable-logging
--ignore-certificate-errors --log-level=0 --login-profile --no-default-browser-check --no-first-run --no-service-autorun --password-store=basic --remote-debugging-port=0
--test-type=webdriver --use-mock-keychain --user-data-dir=/home/user/.config/google-chrome/Profile 1 --flag-switches-begin --flag-switches-end
可执行路径 /opt/google/chrome/google-chrome
配置文件路径 /home/user/.config/google-chrome/Profile 1/Default
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。