Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
7.1k 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 Reply

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...