Getting Selenium to login via .htaccess popup
I am using WebDriver (Selenium2) with Java on linux. I am using WebDriver to aut开发者_如何学编程o fill form and submit it. I am facing problem with htaccess sites i.e., I am not able to access htaccess site through WebDriver.
Can anyone help me out in this regard?
Thanks in advance, Sunil
From Selenium FAQ (which is down at the moment):
How do I use Selenium to login to sites that require HTTP basic authentication (where the browser makes a modal dialog asking for credentials)?
Use a username and password in the URL, as described in RFC 1738: Test Type
open http://myusername:myuserpassword@myexample.com/blah/blah/blahNote that on Internet Explorer this won't work, since Microsoft has disabled usernames/passwords in URLs in IE. However, you can add that functionality back in by modifying your registry, as described in the linked KB article. Set an "iexplore.exe" DWORD to 0 in HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE.
If you don't want to modify the registry yourself, you can always just use Selenium Remote Control, which automatically sets that that registry key for you as of version 0.9.2.
There is an outstanding issue for WebDriver to support basic and digest HTTP authentication. If you want to be notified of changes I suggest voting for the issue at http://code.google.com/p/selenium/issues/detail?id=34
Until there is full support for this across browsers for WebDriver (or Selenium), alternate option is to integrate w/ desktop GUI automation tools, where the desktop GUI tool will automate the HTTP authentication part. You can probably find some examples for this or file downloads, uploads if you google for things like "Selenium AutoIt", etc.
For a cross platform solution, replace AutoIt with Sikuli or something similar.
The solution from the Selenium FAQ does not work - FireFox now adds a prompt to confirm that the user means to authenticate which does not have an obvous-to-me Selenium task.
"You are about to log in to the site "my.domain.com" with the username "myuser"
The cheapest solution is to manually enter the credentials once with the browser profile that the selenium session uses and let the browser save them. (I did this in mid-test) Also added the profile integer value network.http.phishy-userpass-length;255
This other question pointed me at the way to do it programmatically, i.e. using Selenium 2
i'm using this and it's working for me.
public void login(String username, String password){
WebDriver driver = getDriver();
String URL = "http://" + username + ":" + password + "@" + "link";
driver.get(URL);
driver.manage().window().maximize();
}
精彩评论