webdriver对Windows原生程序的支持一直都不是特别好,所以如果在做项目的时候牵扯到对Windows原生程序的操作,我们可能需要借助一些其他手段。
比如某个项目的登录框直接复用的Windows原生登录框,这个就无法被webdriver定位到,这块可以借助Firefox profile来解决。
我们可以从cmd进入Firefox的安装目录,用Firefox.exe -p 来启动Firefox profile的设定框:
可以新建一个,然后把特定的用户名密码保存到浏览器里,这样下次启动该profile的时候,就会弹出已经填好的用户名密码。
然后用下列代码进行调用:
ProfilesIni pi = new ProfilesIni();
FirefoxProfile profile = pi.getProfile("selenium");
System.setProperty("webdriver.gecko.driver","C:/Program Files (x86)/Mozilla Firefox/geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver webdriver = new FirefoxDriver(options);
这样启动的浏览器就是我们已经保存好用户名密码的浏览器了。
我们只要在加载项目页之后做一下处理,就能登陆了。
Alert alert = webdriver.switchTo().alert() ;
alert.accept();