Currently, I can send the user-agent from playwright as well the viewport sizes. But i want to send all the headers informations like accept, accept_encoding, accept_language,referer, cookies,etc
. I looked about setExtraHTTPHeaders()
too. Is there any way to send them in playwright python.
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context(
user_agent=agent #agent declared above,
)
page = context.new_page()
response = page.goto("https://www.somewebsite.com/")
#Doing some work in the website
page.locator('#some_id').fill("some_text")
page.press('#another_id', "Enter")
page.route("**/*", intercept_route)
The answer was found when i looked what is inside dir(context)
. In python, we can use context.set_extra_http_headers(headers)
to send extra headers informations like accept, accept-encoding, referrer, accept-language,etc
user-agent
, prefers-colors-scheme
, accept-language
, and cookies
can be set with the browser's new_context
's arguments user_agent
, color_scheme
, locale
, and storage_state
, respectively. referer
can be set with page.goto
's argument referer
. For other less popular headers that's not mentioned in the documentation , they can be set with new_context
's argument extra_http_headers
. The code can look something similar to
context = browser.new_context(
user_agent=agent,
color_scheme=r"light",
locale=r"en-US,en;q=0.9",
storage_state={cookies: [{}],
extra_http_headers={}
)
page = context.new_page()
response = page.goto(r"https://www.somewebsite.com/",
referer=r"https://google.com"
)
The same can apply for browser.new_page()
. To better see what headers your browser sends you can go to similar sites like this one . Hope that helps, good luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.