1

I am new to selenium and trying to handle authentication header which had been put up on web end for security reason. I am trying to access the site and send data using url using selenium but as authentication is required, I am unable to do so. I've tried below mentioned code but couldn't succeed.

//Selenium-WebDriver Java Code for entering Username & Password as below:
    driver.findElement(By.id("userID")).sendKeys("username");
    driver.findElement(By.id("password")).sendKeys("pass");
    driver.switchTo().alert().accept();
    driver.switchTo().defaultContent();*/
        //self.headers = { "Authorization": "Basic xyz=" };
  /*        driver.switchTo().window("Authentication Required");
        driver.findElement(By.id("userID")).sendKeys("username");
        driver.findElement(By.id("password")).sendKeys("pass");*/

        //selenium.start("addCustomRequestHeader=true");
        //selenium.windowMaximize();
        //selenium.addCustomRequestHeader( "Authorization","Basic "+"xyx=" );

Any help or suggestion would do great. Thanks in advance.

varsha gupta
  • 11
  • 1
  • 4
  • 2
    Possible duplicate of [Selenium - Other way to basic authenticate than via url](https://stackoverflow.com/questions/45345882/selenium-other-way-to-basic-authenticate-than-via-url) – undetected Selenium Nov 16 '17 at 11:35

2 Answers2

0

This is my Java code:

public class LoginPage {

private WebDriver driver;

public LoginPage(WebDriver driver) {
    this.driver = driver;
}

public void login(){
    driver.get("http://localhost:8080/projectLogin/");

    WebElement username = driver.findElement(By.name("iDElementHtmlUser"));
    WebElement pass = driver.findElement(By.name("iDElementHtmlPassword"));

    username.sendKeys("rivanMota");
    pass.sendKeys("1234");
    username.submit();

    boolean logged = driver.getPageSource()
            .contains("idLogged"); // some id that only exists on logged page.
    assertTrue(logged); // True if login success
}
}

And HTML:

<input id="iDElementHtmlUser" type="text" name="username" />
<input id="iDElementHtmlPassword" type="password" name="password" />

That is all folks!

RivanMota
  • 794
  • 7
  • 14
-2

Try this,

WebDriver driver = new ChromeDriver(); 

String baseUrl=”http://” + “USERNAME” + “:” + “PASSWORD” + “@” + “xyz.com”; 

driver.get(baseUrl + “/”);
Pradeep
  • 1
  • 4
  • they removed support for this, read more about it here - https://stackoverflow.com/a/45352660/2447803 – slajma Dec 17 '20 at 16:05