一、启动chrome浏览器
1、启动chrome是需要chromedriver的驱动:
驱动直接可以在selenium的官网就可以找到:http://www.seleniumhq.org/download/
public static void Chrome(){ System.setProperty("webdriver.chrome.driver", "d:\\chromedriver.exe"); //指定驱动路径 WebDriver driver = new ChromeDriver(); driver.get("https://www.baidu.com"); }
********如果不想用setProperty的,那么需要chromedriver.exe ,path可以找到的路径下或者自己添加path路径(怎么添加path具体百度)。
2、让Chrom加载插件:
public static void Chrome(){ System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe"); File file = new File ("D:\\youtube.crx"); ChromeOptions options = new ChromeOptions(); options.addExtensions(file); WebDriver driver = new ChromeDriver(options); driver.get("https://www.baidu.com"); }
3. 修改chrome的请求头:
ChromeOptions options = new ChromeOptions(); options.addArguments("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:69.0) Gecko/20100101 Firefox/69.0"); WebDriver webDriver = new ChromeDriver(options);
4. 禁用js:
ChromeOptions options = new ChromeOptions(); Map<String, Object> prefs = new HashMap<>(); prefs.put("profile.managed_default_content_settings.javascript",2); options.setExperimentalOption("prefs", prefs); WebDriver webDriver = new ChromeDriver(options);
二、启动IE浏览器
1、IE启动也需要下载相应的驱动:
public static void IE(){ System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe"); WebDriver driver = new InternetExplorerDriver(); drivers.get("https://www.baidu.com"); }
2、IE下没有插件加载
3、IE的放大比例为要设置100%
4、启动IE时,需关闭如下图中4个区域的保护模式:
5、对于关闭保护模式,是可以使用代码关闭:
//启动IE浏览器并关闭保护模式 public static void CloseProtectedMode(){ System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe"); DesiredCapabilities dc = DesiredCapabilities.internetExplorer(); dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); //IE默认启动保护模式,要么手动在浏览器的设置中关闭保护模式,要么在代码中加上这一句,即可 dc.setCapability("ignoreProtectedModeSettings", true); WebDriver driver = new InternetExplorerDriver(dc); driver.get("https://www.baidu.com"); }
还没有评论,来说两句吧...