0

So I am trying to login to a site that has it's first login through a popup window, as soon as you visit it, it brings a popup window that requires a User name and password. Now i have a very rough idea on how i would do this if it wasn't a pop up window, but because I don't know how to see the HTML code of the window, i don't know what to put in to driver.findElement(By.id("")) .

Sorry in advance if this is all too vague .

Angelos
  • 3
  • 2
  • Navigate to the site by yourself. Press F12 to see source-code and IDs of the elements. Create your test with a little timeout (for waiting that the window appears) and go for it. – LenglBoy May 15 '18 at 14:34
  • Since the pop up window is the first thing when I visit the site, f12 still shows the source code for the chrome "New tab" window – Angelos May 15 '18 at 14:54
  • When you are opening the URL using driver.get(" ") ,then you are getting the pop up ? – cruisepandey May 15 '18 at 14:55
  • String URL = "http://" + username + ":" + Password + "@" + www.your websitename.com; driver.get(URL); try this – cruisepandey May 15 '18 at 14:58
  • Yep, first thing that happens when I open the url – Angelos May 15 '18 at 14:59
  • For what you recommended above, i get a firefox page that says the connection to the website was reset – Angelos May 15 '18 at 15:03
  • Possible duplicate of [Selenium - Basic Authentication via url](https://stackoverflow.com/questions/45345882/selenium-basic-authentication-via-url) – undetected Selenium May 15 '18 at 15:06
  • Might be useful to mention that the site has 2 logins, the first one is the popup window, and when that is filled in correctly, then there is another login page before moving to the actual site, don't know if that makes a difference regarding the above. – Angelos May 15 '18 at 15:13

2 Answers2

0

You can iterate through the open windows (including the popup) and switch to the desired one (the popup in your case). After that you can use selectors to find the desired elements and enter the login credentials.

Example:

WebDriver driver = ...    

String parentWindowHandle = driver.getWindowHandle();

String popupWindowHandle = null;
Iterator<String> iterator = driver.getWindowHandles().iterator();
while (iterator.hasNext()) {
    popupWindowHandle = iterator.next();
}

driver.switchTo().window(popupWindowHandle);
Ehler
  • 285
  • 3
  • 11
0

So it's not a pop-up, its basic authentication.

Currently, selenium doesn't support basic HTTP authentication (link to issue), but there are workarounds.

In Chrome driver and Firefox driver, you can insert your username and password in the URL.

Example: If you have URL like this: http://the-internet.herokuapp.com/basic_auth

Where username and passwords are both 'admin'

Just change your URL to:

http://admin:admin@the-internet.herokuapp.com/basic_auth

Java code example:

String username = "admin";
String password = "admin";
String url= "http://" + username + ":" + password + "@the-internet.herokuapp.com/basic_auth";
driver.get(url);

This will automatically handle basic auth in chrome and firefox drivers.

More here: Selenium - Basic Authentication via url

Dmitry Shyshkin
  • 384
  • 2
  • 8
  • Thank you , but I get a firefox page saying connection was reset – Angelos May 15 '18 at 16:10
  • What version of FF you use? Also, what version of webdriver and firefox driver? And is this http://admin:admin@the-internet.herokuapp.com/basic_auth also gives you same connection was reset as in your app? – Dmitry Shyshkin May 15 '18 at 16:13
  • Also, are you getting same problem manually, when trying to paste this url in browser? Or just when running tests in selenium? – Dmitry Shyshkin May 15 '18 at 16:15
  • For firefox i use 60.0 (64-bit) . FirefoxDriver geckodriver-v0.20.1-win64 . And I think its Selenium 3.11.0 . Also when i paste the URL with the username and password to the browser, i get the same problem – Angelos May 15 '18 at 16:24
  • Do you get same issue with example url I gave you? http://admin:admin@the-internet.herokuapp.com/basic_auth Or just with your url? Trying to findout if its specific to your app or environment, also, can you share your url? – Dmitry Shyshkin May 15 '18 at 16:47
  • I can login to your URL just fine. Unfortunately i can't share the URL, apologies – Angelos May 15 '18 at 16:57
  • Ok, so now we know its not environment, its your app. Are you getting this "Connection was reset" when entering username and password manually? Are you getting it when you navigate to user:pass@your url manually, using selenium, or both? Are you getting it in chrome too, or just in firefox? – Dmitry Shyshkin May 15 '18 at 17:57
  • Awesome. Glad I could help. BTW. You may ask your developers to whitelist your IP, so you don't even need to do basic auth to access your site – Dmitry Shyshkin May 15 '18 at 20:58