selenium操作chrome时的一些配置
? private void getChromeDriver() {
? ? Config config = new ConfigProvider().get();
? ??
? ? Config configToUse = Optional.ofNullable(config).orElse(ConfigLoader.load());
? ? prepareChromeDriver(configToUse);
? ? final String osName = config.getString("os.name");
? ? ChromeOptions chromeOptions = new ChromeOptions();
? ? if (StringUtils.containsIgnoreCase(osName, "windows")) { // 本地Windows環(huán)境, 初始化local環(huán)境的chrome driver
? ? ? logger.info("windows環(huán)境", osName);
? ? ? chromeOptions.setBinary(configToUse.getString("webdriver.chrome.binary")); // windows下chrome地址
? ? ? chromeOptions.addArguments("--profile-directory=target");
? ? ? chromeOptions.addArguments("--user-data-dir=target\\");
? ? } else { // FAT/UAT/PROD等Linux環(huán)境, 初始化Linux環(huán)境的chrome driver
? ? ? logger.info("Linux環(huán)境", osName);
? ? ? chromeOptions.setBinary("/usr/bin/google-chrome-stable"); // 配置chrome安裝地址
? ? ? chromeOptions.addArguments("--headless");
? ? ? chromeOptions.addArguments("--disable-gpu");
? ? ? String proxyServer = qconfigService.getConfig("proxyHost") + ":" + qconfigService.getConfig("proxyPort"); //代理配置
? ? ? chromeOptions.addArguments("--proxy-server=" + proxyServer);
? ? }
? ? chromeOptions.addArguments("--incognito"); // 打開隱私模式
? ? chromeOptions.addArguments("--lang=" + chineses); // 中文
? ? chromeOptions.addArguments("-test-type", "--ignore-certificate-errors"); // 忽略證書錯(cuò)誤
? ? chromeOptions.addArguments("--no-sandbox");
//? ? 添加chrome模擬器
//? ? Map<String, String> mobileEmulation = new HashMap<String, String>();
//? ? mobileEmulation.put("deviceName", "Galaxy S5");//"Nexus 5X");
//? ? chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
? ??
? ? String webDriverPath = System.getProperty("webdriver.chrome.driver");
? ? ChromeDriverService driverService = new ChromeDriverService.Builder().usingDriverExecutable(new File(webDriverPath)).usingAnyFreePort().build();
? ? try {
? ? ? driverService.start();
? ? } catch (IOException e) {
? ? ? logger.error(e);
? ? }
? ? logger.info("webdriver.chrome.driver", webDriverPath);
? ? ChromeDriver driver = new ChromeDriver(driverService, chromeOptions);
? ? logger.info("webdriver.chrome.driver", webDriverPath);
? }
總結(jié)
以上是生活随笔為你收集整理的selenium操作chrome时的一些配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无锁数据结构二-乱序控制(栅栏)
- 下一篇: 无锁数据结构三:无锁数据结构的两大问题