How to handle browser authentication in Chrome using Selenium Webdriver (C#)?

14,932

Try getting the URL like this...

driver.get("http://username:[email protected]");

Using the Alert class like this post here(How to handle authentication popup with Selenium WebDriver using Java) apparently only works in IE.

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
        IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
        alert.SetAuthenticationCredentials("username", "password")
Share:
14,932

Related videos on Youtube

Ashish Sharma
Author by

Ashish Sharma

I am an Asst. Manager Software Testing and also have good hand-on experience in .NET Framework C#. Area of expertise: 1. .Net Framework C# 2. SQL Server 2005, 2008 3. Manual Testing 4. Basic knowledge of JAVA, C++, & QTP.

Updated on June 04, 2022

Comments

  • Ashish Sharma
    Ashish Sharma almost 2 years

    I am writing an automation script for Chrome browser in selenium web driver using C#. I got stuck in a scenario where multiple tabs are getting open in the same browser and I need to navigate to the first Tab of a browser and need to re-enter the login credentials in the authentication dialog box.

    Please find the below screenshot for authorization window:

    enter image description here

    I am unable to navigate to the first tab and unable to pass the username & password. I found some answers in the Stackoverflow and tried in my script but nothing went right. Here is my sample code:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
    alert.SetAuthenticationCredentials(username, pwd);
    

    After executing the above code, the following error is coming:

    WebDriverWait has some invalid arguments. Argument '2': cannot convert from 'int' to 'System.TimeSpan'

    Is there any specific code for Chrome browser? I am using Visual studio 2008.

    • JeffC
      JeffC almost 7 years
    • Ashish Sharma
      Ashish Sharma almost 7 years
      As I mentioned that i searched some threads in Stackoverflow but it did not work in my script. So there is no point to mark it duplicate.
    • JeffC
      JeffC almost 7 years
      See: How do I do X? The expectation on SO is that the user asking a question not only does research to answer their own question but also shares that research, code attempts, and results. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: How to Ask
    • JeffC
      JeffC almost 7 years
      You mentioned it but you didn't provide any details. Until you do some research and actually provide details that demonstrate how the solutions provided elsewhere don't work here and the results, it hasn't been proven that it's not a duplicate.
    • undetected Selenium
      undetected Selenium over 6 years
  • Ashish Sharma
    Ashish Sharma almost 7 years
    Hi Cavan thanks for your reply, I tried your line of code but it gave me an error. WebDriverWait wait = new WebDriverWait(driver, 10); IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent()); alert.SetAuthenticationCredentials(UserLogin.username, UserLogin.pwd); - Error "Argument '2': cannot convert from 'int' to 'System.TimeSpan'". Is there any specific code for Chrome browser?
  • Cavan Page
    Cavan Page almost 7 years
    I have updated code to c#. before it was java which had slightly different syntax.
  • Ashish Sharma
    Ashish Sharma almost 7 years
    Thanks Cavan, I tried with driver.Navigate().GoToUrl("username:[email protected]"‌​) with some tricks and I am able to handle the authentication alert window.
  • Ashish Sharma
    Ashish Sharma almost 7 years
    Thanks @smith9234 for your reply. I tried driver.Navigate().GoToUrl("username:[email protected]"‌​‌​) with some tricks and able to handle the authentication dialog box.