[英]How to set headers in python selenium chrome webdriver
I cant find how to set request headers within selenium chrome webdriver (python).我找不到如何在 selenium chrome webdriver (python) 中设置请求标头。 I need to set "host" header. I tryed that:我需要设置“主机”header。我试过了:
from selenium import webdriver
from pyvirtualdisplay import Display
from selenium.webdriver import DesiredCapabilities
url = 'http://localhost:888/test'
display = Display(visible=0, size=(800, 800))
display.start()
desired_capabilities = DesiredCapabilities.CHROME.copy()
desired_capabilities['chrome.page.customHeaders.host'] = 'test.local'
driver = webdriver.Chrome('/tmp/chromedriver',
desired_capabilities=desired_capabilities)
driver.get(url)
And result:结果:
GET /test HTTP/1.1
Host: localhost:888
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/53.0.2785.143 Chrome/53.0.2785.143 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
So, I expected header host:test.local
but found that Host: localhost:888
所以,我预计 header host:test.local
但发现Host: localhost:888
What I am doing wrong?我做错了什么? I cant find how to set headers with Chrome webdriver:(我找不到如何使用 Chrome webdriver 设置标头:(
You can change the Headers in continue_request()
at my_headers = {"sec-ch-ua-platform": "Android"}
.您可以在my_headers = {"sec-ch-ua-platform": "Android"}
处更改continue_request()
中的标头。
# resource: https://stackoverflow.com/questions/66227508/selenium-4-0-0-beta-1-how-add-event-listeners-in-cdp
class cdp_listener:
from typing import Dict
def __init__(self):
self.listeners = {}
async def async_helper(self):
async with driver.bidi_connection() as connection:
session, devtools = connection.session, connection.devtools
for listener in self.listeners.items():
my_listener = await listener[1]["listener"](connection=connection)
async for event in my_listener:
try:
await session.execute(listener[1]["at_event"](event=event, connection=connection))
except Exception as e:
print(e)
def trio_helper(self):
import trio
trio.run(self.async_helper)
def start_threaded(self, listeners: Dict[str,Dict[callable, callable]] = {}):
if listeners:
self.listeners = listeners
import threading
thread = threading.Thread(target=self.trio_helper)
thread.start()
return thread
def add_listeners(self, listeners: Dict[str,Dict[callable, callable]]):
self.listeners = listeners
def remove_listener(self, listener:str):
del self.listeners[listener]
async def all_requests(connection):
session, devtools = connection.session, connection.devtools
pattern = map(devtools.fetch.RequestPattern.from_json,[{"urlPattern":"*"}])
pattern = list(pattern)
await session.execute(devtools.fetch.enable(patterns=pattern))
return session.listen(devtools.fetch.RequestPaused)
def continue_request(event, connection):
print({"type":event.resource_type.to_json(),"frame_id": event.frame_id, "url": event.request.url})
session, devtools = connection.session, connection.devtools
headers = event.request.headers.to_json()
my_headers = {"sec-ch-ua-platform": "Android"}
headers.update(my_headers)
my_headers = []
for item in headers.items():
my_headers.append(devtools.fetch.HeaderEntry.from_json({"name": item[0], "value": item[1]}))
return devtools.fetch.continue_request(request_id=event.request_id, headers=my_headers)
cdp_listener = cdp_listener()
thread = cdp_listener.start_threaded(listeners= {"continue":{"listener":all_requests,"at_event":continue_request}})
driver.get('https://modheader.com/headers?product=ModHeader')
请求标头中的Host
是服务器(目的地)的名称,由您的driver.get(url)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.