← Back to Discord Forum

Is it possible to assign multiple origins to http_credentials?

bandito9274posted in #help-playwright
Open in Discord

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?"

4 replies

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

  1. Create a function to encode the username/password

`import base64

user_pass = "myuser:mypass"
encoded = base64.b64encode(user_pass.encode())
    {"Authorization": "Basic " + encoded.encode()}

2. Call the function before opening the page (this will add the auth header and login with basic auth)

The problem with this solution, that the header doesn't give access to the resources to the page...

Related Discord Threads

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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 luc@ray.run.