硒请求HTTP标题中缺少参考器
我正在用硒编写一些测试并注意到,标题中缺少 Referer
。 最小示例,用 https://httpbin.org/headers
import selenium.webdriver
options = selenium.webdriver.FirefoxOptions()
options.add_argument('--headless')
profile = selenium.webdriver.FirefoxProfile()
profile.set_preference('devtools.jsonview.enabled', False)
driver = selenium.webdriver.Firefox(firefox_options=options, firefox_profile=profile)
wait = selenium.webdriver.support.ui.WebDriverWait(driver, 10)
driver.get('http://www.python.org')
assert 'Python' in driver.title
url = 'https://httpbin.org/headers'
driver.execute_script('window.location.href = "{}";'.format(url))
wait.until(lambda driver: driver.current_url == url)
print(driver.page_source)
driver.close()
<html><head><link rel="alternate stylesheet" type="text/css" href="resource://content-accessible/plaintext.css" title="Wrap Long Lines"></head><body><pre>{
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"Connection": "close",
"Host": "httpbin.org",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0"
}
}
</pre></body></html>
我写了以下 没有参考器
。 手动执行
window.location.href = "https://httpbin.org/headers"
但是,如果我浏览到任何页面并在Firefox控制台中
,则 Referer
die 会按预期出现。正如下面注释中指出的那样,当使用
driver.get("javascript: window.location.href = '{}'".format(url))
而不是
driver.execute_script("window.location.href = '{}';".format(url))
请求时确实包括 Referer
。另外,当使用Chrome而不是Firefox时,这两种方法都包括参考器
。
因此,主要问题仍然存在:如上所述,使用firefox发送时,请求中 为什么缺少?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
推荐人
根据MDN文档来源:
来源:
隐私和安全问题
与
Referer
http Header相关的隐私和安全风险:来源:
从
Referform
header profective从repective中解决安全问题
,可以按照步骤来降低大多数安全风险:
来源:
yousecase
我已经通过geckodriver/firefox and chromedriver/ 执行了您的代码铬组合:
代码块:
观察:
使用geckodriver/firefox
参考器:“ https://www.python.org/”
header 缺少如下:< /p>使用chromedriver/chrome
推荐人:“ https://www.python.org/”
标题为 present 如下:结论:
似乎在处理
推荐人
标题时,成为Geckodriver/Firefox的问题。outro
推荐人策略
Referer
as per the MDN documentationSource: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer
However:
Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer
Privacy and security concerns
There are some privacy and security risks associated with the
Referer
HTTP header:Source: https://developer.mozilla.org/en-US/docs/Web/Security/Referer_header:_privacy_and_security_concerns#The_referrer_problem
Addressing the security concerns
From the
Referer
header perspective majority of security risks can be mitigated following the steps:Sources:
This usecase
I have executed your code through both through GeckoDriver/Firefox and ChromeDriver/Chrome combination:
Code Block:
Observation:
Using GeckoDriver/Firefox
Referer: "https://www.python.org/"
header was missing as follows:Using ChromeDriver/Chrome
Referer: "https://www.python.org/"
header was present as follows:Conclusion:
It seems to be an issue with GeckoDriver/Firefox in handling the
Referer
header.Outro
Referrer Policy