Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

java - Webdriver - HTTP authentication dialog

I have a very simple selenium-webdriver script. I would like to do HTTP authentication using webdriver.

Script:

WebDriver driver = new FirefoxDriver();  
driver.get("http://www.httpwatch.com/httpgallery/authentication/");
driver.findElement(By.id("displayImage")).click();
Thread.sleep(2000);
driver.switchTo().alert().sendKeys("httpwatch");

Issue:

driver.switchTo().alert().sendKeys("httpwatch");

throws

org.openqa.selenium.NoAlertPresentException: No alert is present

Question:

  • Does Webdriver find only an alert dialog as alert?
  • What are my options to automate this without using AutoIt OR http:// username:password @somesite

EDIT

Alert has below method and does not seem to have been implemented yet.

driver.switchTo().alert().authenticateUsing(new UsernameAndPassword("username","password"))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that this is not a javascript popup hence you cannot manipulate it via selenium's alert().

If both AutoIt and submitting credentials in the URL (the easiest option - just open up the url and click "Display Image") are not options for you, another approach could be to use AutoAuth firefox addon to automatically submit the previously saved credentials:

AutoAuth automatically submits HTTP authentication dialogs when you’ve chosen to have the browser save your login information. (If you’ve already told the browser what your username and password are, and you’ve told it to remember that username and password, why not just have it automatically submit it instead of asking you each time?)

Following the answer suggested in HTTP Basic Auth via URL in Firefox does not work? thread:

  • 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);
driver = new FirefoxDriver(firefoxProfile);

Also, in a way similar to AutoIt option - you can use sikuli screen recognition and automation tool to submit the credentials in the popup.


Also see other ideas and options:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...