我试图使用 addCustomRequestHeader 方法为 selenium 请求设置自定义 header 。下面给出源代码
Selenium sel = new DefaultSelenium("localhost",4444,"*firefox","http://www.google.com");
sel.start("addCustomRequestHeader=true");
// sel.start();
sel.addCustomRequestHeader("mycustomheader","automation");
sel.open("http://www.google.com/");
此代码未将 header 添加到请求中。我尝试使用 Fiddler 查找请求 header 。这里有人知道我在这里做错了什么吗?任何帮助将不胜感激
最佳答案
您需要以代理注入(inject)模式启动selenium
java -jar selenium-server-standalone.jar -proxyInjectionMode
然后您可以像这样添加自定义请求 header (在 Python 中)
sel.start("addCustomRequestHeader=true")
sel.add_custom_request_header("mycustomheader","automation")
sel.open('http://www.google.com')
要查看是否已应用自定义 header ,请检查正在运行 selenium 服务器的选项卡。您应该在控制台消息中看到类似的内容
INFO - Command request: addCustomRequestHeader[mycustomheader, automation] on session
INFO - Got result: OK on session
关于selenium - 如何在selenium中使用addCustomRequestHeader方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7196775/