Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 923
Ashish Sharma Avatar asked Oct 26 '25 10:10

Ashish Sharma


1 Answers

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")
like image 137
Cavan Page Avatar answered Oct 29 '25 00:10

Cavan Page



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!