在硒中设置请求标头 [英] setting request headers in selenium

查看:81
本文介绍了在硒中设置请求标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将请求标头"Referer"设置为欺骗来自另一个站点的请求.我们需要对使用特定引荐来源网址的能力进行测试,该引荐来源网址将向用户返回特定的表单,否则将给出替代的表单.

I'm attempting to set the request header 'Referer' to spoof a request coming from another site. We need the ability test that a specific referrer is used, which returns a specific form to the user, otherwise an alternative form is given.

我可以通过以下方式在poltergeist中做到这一点:

I can do this within poltergeist by:

page.driver.headers = {"Referer" => referer_string}

但是我找不到selemium驱动程序的等效功能.

but I can't find the equivalent functionality for the selemium driver.

如何在capybara硒驱动程序中设置请求标头?

How can I set request headers in the capybara selenium driver?

推荐答案

Webdriver不包含用于执行此操作的API.请参阅来自Selenium跟踪器的问题141 以获得更多信息.该问题的标题说这与响应头有关,但是在此问题范围内,Selenium决定不包含用于请求头的API.关于添加API来设置请求标头的几个问题已标记为重复:第一第二

Webdriver doesn't contain an API to do it. See issue 141 from Selenium tracker for more info. The title of the issue says that it's about response headers but it was decided that Selenium won't contain API for request headers in scope of this issue. Several issues about adding API to set request headers have been marked as duplicates: first, second, third.

以下是我可以提出的几种可能性:

Here are a couple of possibilities that I can propose:

  1. 使用其他驱动程序/库代替硒
  2. 编写一个特定于浏览器的插件(或找到一个现有的插件),该插件可让您添加请求标头.
  3. 使用 browsermob-proxy 或其他代理.
  1. Use another driver/library instead of selenium
  2. Write a browser-specific plugin (or find an existing one) that allows you to add header for request.
  3. Use browsermob-proxy or some other proxy.

在大多数情况下,我会选择选项3.不难.

I'd go with option 3 in most of cases. It's not hard.

请注意, Ghostdriver为此提供了API ,但其他驱动程序不支持该API.

Note that Ghostdriver has an API for it but it's not supported by other drivers.

这篇关于在硒中设置请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆