# Browser类型 # Firefox BrowserType=Firefox DriverPath=src/main/resourcec/geckodriver.exe # Chrome #BrowserType=Chrome #DriverPath=src/main/resources/chromedriver.exe
public static WebDriver driver; //封装driver public static String configurationFile(String key) throws IOException { //封装读取配置文件 String filePath = System.getProperty("user.dir")+"\\webDriver.properties"; Properties properties = new Properties(); FileInputStream fileInputStream = new FileInputStream(filePath); properties.load(fileInputStream); fileInputStream.close(); if (key == null || key.equals("") || key.equals("null")){ return ""; } String result; result = properties.getProperty(key); return result; } public static void openBaidu() throws InterruptedException, IOException { //封装打开百度首页 String Url = "https://www.baidu.com"; String browserType = configurationFile("BrowserType"); if (browserType.equals("Firefox")) { System.setProperty("webdriver.firefox.marionette", configurationFile("DriverPath")); driver = new FirefoxDriver(); }else if (browserType.equals("Chrome")){ System.setProperty("webdriver.chrome.driver", configurationFile("DriverPath")); ChromeOptions options = new ChromeOptions(); options.addArguments("disable-infobars"); driver =new ChromeDriver(options); } driver.get(Url); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); Thread.sleep(2000); }