最近在做一个测试,需要通过安全验证登陆后台后才能继续后面的操作。
解决办法:
方法一:在URL里面直接加入用户名和密码:
driver.get("https://username:password@monitor.test.com/nagios/");
这样就能直接访问到了!!
方法二:对于IE浏览器需要修改注册表
保存下面内容保存为reg,双击
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE]
"iexplore.exe"=dword:00000000
方法三:借助第三方工具如autoit。
参见这位大神的方法:http://blog.csdn.net/caiqcong/article/details/7600988
方法四:
很不幸,方法一中如果username是domain\username这样的格式,那么\是会被浏览器给解码成%5C这样的。这个时候可以用启动同一个firefox的profile来实现。
具体参考:
http://stackoverflow.com/questions/3021602/http-basic-auth-via-url-in-firefox-does-not-work
For some reason (maybe the ones explained at http://stackoverflow.com/a/14348701/256245), the above solution does not work with newer versions of Firefox. Here is what works for me now (tested with Firefox 19.0.2):
- Install AutoAuth Firefox plugin;
- Visit the site where the authentication is needed. Enter your username and password and make sure to choose to save the credentials;
- Save AutoAuth installation file at your hard drive: at the plugin page, right click at “Add to Firefox” and “Save link as”;
-
Instantiate Firefox webdriver as following:
FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default"); File pluginAutoAuth = new File("src/test/resources/autoauth-2.1-fx+fn.xpi"); firefoxProfile.addExtension(pluginAutoAuth); return new FirefoxDriver(firefoxProfile);
第四步中default可以定制自己一个profile,
File pluginAutoAuth = new File("src/test/resources/autoauth-2.1-fx+fn.xpi");
firefoxProfile.addExtension(pluginAutoAuth);
这两句经实践是可以去掉的,因为第一行中是那个default的profile中是有安装这个插件的。
插件地址:https://addons.mozilla.org/en-us/firefox/addon/autoauth/
这个问题其实讨论也很多,以下一些地址保存后续参考用:
https://code.google.com/p/selenium/issues/detail?id=34
http://stackoverflow.com/questions/11522434/how-to-handle-login-pop-up-window-using-selenium-webdriver
http://sqa.stackexchange.com/questions/2277/using-selenium-webdriver-with-windows-authentication
http://stackoverflow.com/questions/12151958/login-popup-window-using-selenium-webdriver