Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium - Basic Authentication via url

In my Selenium-Test (with chromedriver-2.24) I'm trying to access my webpage via basic authentication with the following statement:

WebDriver driver  = ...;
driver.get("http://admin:admin@localhost:8080/project/");

But Google Chrome gives me the following warning in the console:

[Deprecation] Subresource requests whose URLs contain embedded credentials (e.g. https://user:pass@host/) are blocked. See https://www.chromestatus.com/feature/5669008342777856 for more details.

In the tagged link is mentioned that the support was dropped:

Drop support for embedded credentials in subresource requests. (removed)

My question now is, is there an other way to basic-authenticate from Selenium?

NOTE: this has not helped: How to Handle HTTP Basic Auth headers in Selenium Webdriver using Java ?

like image 612
Lino Avatar asked Jul 27 '17 08:07

Lino


2 Answers

Selenium 4 supports basic authentication

WebDriver driver = new ChromeDriver();
HasAuthentication authentication = (HasAuthentication) driver;
authentication.register(() -> new UsernameAndPassword("username", "pwd"));
driver.get("your-site.com");

https://www.selenium.dev/blog/2021/a-tour-of-4-authentication/

like image 58
Sujit Avatar answered Oct 13 '22 07:10

Sujit


The basic authentication via url is blocked only for sub resources. So you could still use it on the domain:

driver.get("http://admin:admin@localhost:8080");
driver.get("http://localhost:8080/project");

You could also create a small extension to automatically set the credentials when they are requested:

options = webdriver.ChromeOptions()
options.add_extension(r'C:\dev\credentials.zip')

https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46

like image 31
Florent B. Avatar answered Oct 13 '22 08:10

Florent B.