gpt4 book ai didi

python - 如何在 python selenium chrome webdriver 中设置标题

转载 作者:行者123 更新时间:2023-11-28 19:08:37 25 4
gpt4 key购买 nike

我找不到如何在 selenium chrome webdriver (python) 中设置请求 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)

结果:

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

所以,我期望 header host:test.local 但发现 Host: localhost:888

我做错了什么?我找不到如何使用 Chrome webdriver 设置 header :(

最佳答案

请求 header 中的

Host服务器(目的地)的名称,由您的 driver.get(url) 设置.

关于python - 如何在 python selenium chrome webdriver 中设置标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43391941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com