Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
6.7k views
in Technique[技术] by (71.8m points)

visual studio - How to manage Basic Authentication login with selenium c#?

I'm making tests for the company where i'm doing my intership, but i don't know how to make that the script put my credentials in this part, if i enter in the page(myself, not the chromedriver) automatically gets the credentials from the login on my pc, obviously the test driver can't do that, that's why i get that alert, my question is how can i put the credentials there automatically? for now i'm putting a thread sleep and putting the credentials manually but i know that's not the optimal way to do it, i already try using the alert methods:

var alert = driver.SwitchTo().Alert();
alert.SetAuthenticationCredentials("user","password");
alert.Accept();

But I get this error:

Message: OpenQA.Selenium.NoAlertPresentException : no such alert
(Session info: chrome=87.0.4280.141)

And if i put a wait i got a timeout but nothing happen, as you can see i'm using chrome, i read a post from some years that chrome can't manage alerts, but i don't know if that's a thing today, i hope you guys can help me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's not an alert as such but the Basic Authentication popup.


Solution

You can access the url passing the username and password embedded within the url as follows:

driver.Navigate().GoToUrl("http://<username>:<password>@website.com/");

As an example to login within Basic Auth you can use:

driver.Navigate().GoToUrl("http://admin:[email protected]/basic_auth");

References

You can find a couple of relevant detailed discussions in:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...