SpringBoot整合Selenium

package com.vmware.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @apiNote SpringBoot整合Selenium
 * @author 冰点契约 2022-05-03
 */
@Configuration(proxyBeanMethods = true)
@ConfigurationProperties(prefix = "selenium-config")
@Slf4j
@ConditionalOnClass(value = {
        WebDriver.class
})
@Data
public class SeleniumConfig {
    //浏览器语言
    private String lang;
    //驱动路径
    private String driverPath;
    //窗口是否最大化
    private Boolean windowMax;

    @Bean(name = {"driver"})
    public ChromeDriver chromeDriver() {
        System.setProperty("webdriver.chrome.driver", driverPath);
        ChromeDriver driver = new ChromeDriver(chromeOptions());
        if (windowMax) {
            driver.manage().window().maximize();
        }
        log.info("chrome driver init success!");
        return driver;
    }

    @Bean
    public ChromeOptions chromeOptions() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--lang=" + lang);
        options.addArguments("ignore-certificate-errors");
        return options;
    }
}
Logo

苏州本地的技术开发者社区,在这里可以交流本地的好吃好玩的,可以交流技术,可以交流招聘等等,没啥限制。

更多推荐