Reputation: 21
I have problem with basic auth. Earlier I used typical authorization by link - user:password@url, but for some reason I need to change to other way.
I want to achieve something similar to this https://medium.com/automationmaster/handling-basic-authentication-window-with-selenium-webdriver-and-devtools-api-ec716965fdb6
public void setup() {
// Setup Chrome driver
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, SECONDS);
String username = "admin";
String password = "admin";
DevTools devTools = driver.getDevTools();
devTools.createSession();
devTools.send(Network.enable(Optional.of(100000), Optional.of(100000), Optional.of(100000)));
String auth = username + ":" + password;
String encodeToString = Base64.getEncoder().encodeToString(auth.getBytes());
// Pass the network header -> Authorization : Basic <encoded String>
Map<String, Object> headers = new HashMap<>();
headers.put("Authorization", "Basic " + encodeToString);
devTools.send(Network.setExtraHTTPHeaders(new Headers(headers)));
driver.get("https://the-internet.herokuapp.com/basic_auth");
but I cant convert that idea to python.
I have tried several option but none of it worked.
In my code a I have a method used to get url
with authentication one of my ideas looks something like that:
def get_url_with_authentication(self):
username = xyz
password = zxc
auth = username + ":" + password
encodeToString = base64.b64encode(auth.encode())
encodeToString = str(encodeToString)
return "https://" + username + ":" + password + "@" + self.__url + "/" + encodeToString
Upvotes: 1
Views: 1226
Reputation: 506
As of the current state of Selenium 4, this is only possible in Java. https://www.selenium.dev/documentation/webdriver/bidirectional/bidi_api_remotewebdriver/ The better option to achieve that would be to obtain session data using requests library and then inject it into Selenium session cookie.
Upvotes: 1