According to the docs, it is possible to add http_credentials to new_context https://playwright.dev/python/docs/api/class-browser#browser-new-context
Like
context = browser.new_context(http_credentials={ "username": "user", "password": "pass", "origin": "http://domiain.com/protected" })
The question is: Is it possible to assign multiple origins to http_credentials? I have another URL which should be authenticate with the same user/password.
This thread is trying to answer question "Is it possible to assign multiple origins to http_credentials in Playwright?"
The origin has to be a string if i m correct the origin might be evaluated here (?) https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/fetch.ts#L443
Would be leaving origin undefined work for you? : 'If no origin is specified, the username and password are sent to any servers upon unauthorized responses.'
Thanks, yes, this works, until I have only one user/pass. And sending unnecessarily to all the hosts... The problem is, that I have different host, with different user/pass within my script, so I cannot define just one of them... Maybe, I will switch to new_page - that also have the http_credentials parameter to add
Okay, I figured it out... with using set_extra_http_headers
`import base64
user_pass = "myuser:mypass"
encoded = base64.b64encode(user_pass.encode())
page.set_extra_http_headers(
{"Authorization": "Basic " + encoded.encode()}
)`
2. Call the function before opening the page (this will add the auth header and login with basic auth)
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].