1

I'm trying to handle Authenticate Required window using selenium. I will put my code belowe, but I have some problem, because it fill up holders to put login and password but when it's end filling up and should click ENTER suddenly pops up another authenticate Required window with default values. I'm trying two ways but it does not work. Also I'm trying use some AutoItX but nothing. I need to automize it as well as it's possible.

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.security.UserAndPassword;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.atos.raport.data.LoadData;
import com.atos.raport.model.User;
import com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent;
import com.thoughtworks.selenium.webdriven.commands.RunScript;

public class RaportGenerator {

    private static final String PROPERTY_PATH = "C:\\Users\\A673113\\Desktop\\geckodriver.exe";
    private static Logger LOG = Logger.getLogger(RaportGenerator.class.getName());

    public static void main(String[] args) throws InterruptedException, AWTException {

        LoadData ld = new LoadData();
        Robot robot = new Robot();

        ld.loadLinksData();
        ld.loadUserData();

        File firefoxDriver = new File(PROPERTY_PATH);
        System.setProperty("webdriver.gecko.driver", firefoxDriver.getAbsolutePath());

        ProfilesIni profile = new ProfilesIni();

        final FirefoxProfile firefoxProfile = profile.getProfile("default"); // ustawienie defaultowych ustawien dla
                                                                                // przegladarki
        firefoxProfile.setPreference("xpinstall.signatures.required", false);
        WebDriver driver = new FirefoxDriver(firefoxProfile);

        driver.get(GlobalData.linksList.get(0).getUrlAddresToSVN());

        StringSelection user = new StringSelection(GlobalData.userList.get(0).getLogin());
        System.out.println(user);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(user, null);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyRelease(KeyEvent.VK_TAB);
        Thread.sleep(2000);

        StringSelection pass = new StringSelection(GlobalData.userList.get(0).getPassword());
        System.out.println(pass.toString());
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pass, null);

        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);

        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);

        /*
         * WebDriverWait wait = new
         * WebDriverWait(driver, 20); Alert alert =
         * wait.until(ExpectedConditions.alertIsPresent()); try { alert =
         * driver.switchTo().alert(); // zmiana na okno popup Runtime.getRuntime().exec(
         * "C:\\Users\\A673113\\Desktop\\SVN_Raport_generator\\danielzarz.exe");//
         * wczytanie // pliku z // danymi // trzeba to // zautomatyzowac alert.accept();
         * // klikniecie ok na popupie } catch (IOException e) { LOG.log(null,
         * e.getMessage().toString());
         * 
         * }
         */

    }

}
artist
  • 319
  • 1
  • 5
  • 18
  • Possible duplicate of [Handling Browser Authentication using Selenium](https://stackoverflow.com/questions/10395462/handling-browser-authentication-using-selenium) – JeffC Sep 06 '17 at 14:47

1 Answers1

0

Could you pass both the username and password into the URL when you attempt to open it e.g. http://username:password@example.com/

If this doesn't work, could you ask one of the Dev's to whitelist your IP?

I had a similar issue, and white listing my IP seemed to be the easiest fix.

Danny
  • 287
  • 2
  • 15
  • @artist I think you may need to get one of the Developers to white list your IP address then, I had the same issue along time ago. This should be very easy for them to do. – Danny Sep 06 '17 at 11:21