setting request headers in selenium

后端 未结 9 1185
青春惊慌失措
青春惊慌失措 2020-11-29 21:53

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

9条回答
  •  抹茶落季
    2020-11-29 22:45

    If you use the HtmlUnitDriver, you can set request headers by modifying the WebClient, like so:

    final case class Header(name: String, value: String)
    
    final class HtmlUnitDriverWithHeaders(headers: Seq[Header]) extends HtmlUnitDriver {
      super.modifyWebClient {
        val client = super.getWebClient
        headers.foreach(h => client.addRequestHeader(h.name, h.value))
        client
      }
    }
    

    The headers will then be on all requests made by the web browser.

提交回复
热议问题