一般在启动浏览器的时候,直接进行new ChromeDriver()就表示启动相关类型的浏览器,这样比较简单。如果想要更进一步的设置,则需要对浏览器的启动配置项进行设置。因为selenium webdriver是基于Firefox开发的。2.0版本之前不需要相关driver进行驱动,3.0时做了改动,火狐浏览器也需要相关driver进行驱动

下面是driver下载路径

chromedriver:

http://npm.taobao.org/mirrors/chromedriver/

ieDrvier:

http://selenium-release.storage.googleapis.com/index.html

 

IE启动注意点:

DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();

ieCapabilities.setCapability(InternetExplorerDriver.IE_SWITCHES, “-private”);

“InPrivate 浏览”可帮助阻止 Internet Explorer 存储你的浏览会话的数据。这包括 Cookie、Internet 临时文件、历史记录以及其他数据。默认情况下将禁用工具栏和扩展。有关详细信息,请参阅帮助。

关闭此浏览器窗口,以关闭 InPrivate 浏览。

ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);

 火狐启动注意点:

火狐的profile安装的位置,在help–》troubleshooting information–》show folder
webdriver.accept.untrusted.certs的属性是忽略证书。

  1. FilePath.getdriverDirectory()是获取driver的路径,可自行配置,
  1. public class lanchBrowser {
  2. static Logger logger = Logger.getLogger(lanchBrowser.class );
  3. public static WebDriver driver=null;
  4. public static FirefoxProfile firefoxProfile=null;
  5. public static DesiredCapabilities caps=null;
    //此处表示启动node节点下的浏览器。后续会讲解,暂时用不到
  6. public static String nodeurl="";

  7. //根据传入的类型来其他不同的浏览器
  8. public static WebDriver getBrowserDriver(String browserType) {
  9. switch (browserType) {
  10. case "firefox":
  11. System.setProperty("webdriver.firefox.bin",FilePath.getdriverDirectory()+"FireFsoxDriverServer.exe");
  12. firefoxProfile.setPreference("webdriver.accept.untrusted.certs", "true");
  13. caps.setCapability(FirefoxDriver.PROFILE,firefoxProfile);
  14. if (nodeurl.equals("")) {
  15. driver=new FirefoxDriver(firefoxProfile);
  16. }else {
  17. try {
  18. driver=new RemoteWebDriver(new URL(nodeurl), caps);
  19. } catch (MalformedURLException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. logger.info("runDriver is firefox.Open FireFox browser.....");
  24. break;
  25. case "ie":
  26. if (File.separator.equals("\\")) {
  27. System.setProperty("webdriver.ie.driver", FilePath.getdriverDirectory()+ "IEDriverServer.exe");
  28. } else if (File.separator.equals("/")) {
  29. System.setProperty("webdriver.ie.driver", FilePath.getdriverDirectory() + "IEDriverServer.exe");
  30. }
  31. caps=DesiredCapabilities.internetExplorer();
  32. caps.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, false);
  33. caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
  34. caps.setCapability(InternetExplorerDriver.IE_SWITCHES, ".private");
  35. if (nodeurl.equals("")) {
  36. driver=new InternetExplorerDriver(caps);
  37. }else {
  38. try {
  39. driver=new RemoteWebDriver(new URL(nodeurl), caps);
  40. } catch (MalformedURLException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. logger.info("runDriver is ie.Open IE browser.....");
  45. break;
  46. case "chrome":
  47. if (File.separator.equals("\\")) {
  48. System.setProperty("webdriver.chrome.driver", FilePath.getdriverDirectory()+ "chromedriver.exe");
  49. } else if (File.separator.equals("/")) {
  50. System.setProperty("webdriver.chrome.driver", FilePath.getdriverDirectory() + "chromedriver");
  51. }
  52. new DesiredCapabilities();
  53. caps=DesiredCapabilities.chrome();
  54. caps.setCapability("chrome.switches",Arrays.asList("--start-maximized"));
  55. // caps.setCapability("chrome.switches",Arrays.asList("--proxy-server=http://url:port"));设置代理
  56. if (nodeurl.equals("")) {
  57. driver=new ChromeDriver(caps);
  58. }else {
  59. try {
  60. driver=new RemoteWebDriver(new URL(nodeurl), caps);
  61. } catch (MalformedURLException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. logger.info("runDriver is chrome.Open Chrome browser.....");
  66. break;
  67. }
  68. return driver;
  69. }
  70. }

 

版权声明:本文为AllenRandolph原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/AllenRandolph/p/9760942.html