在Selenium中发出POST请求而不填写表格? [英] Making a POST request in Selenium without filling a form?

查看:1161
本文介绍了在Selenium中发出POST请求而不填写表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序A,该应用程序应处理使用POST方法提交的表单.发起请求的实际表单是在完全独立的应用程序B中.我正在使用Selenium测试应用程序A,我想编写一个测试用例以进行表单提交处理.

I have an application A that should handle a form submit made with POST method. The actual form, that initiates the request, is in totally separate application B. I am testing application A using Selenium, and I like to write a test case for form submit handling.

如何执行此操作?可以在Selenium中完成吗?应用程序A没有可以启动此请求的表单.

How to do this? Can this be done in Selenium at all? Application A does not have a form that can initiate this request.

请注意,该请求必须使用POST,否则我可以只使用WebDriver.get(url)方法.

Note, that the request must use POST, otherwise I could just use WebDriver.get(url) method.

推荐答案

使用硒,您可以执行任意Javascript,包括

With selenium you can execute arbitrary Javascript including programmatically submit a form.

使用Selenium Java最简单的JS执行:

Simplest JS execution with Selenium Java:

if (driver instanceof JavascriptExecutor) {
    System.out.println(((JavascriptExecutor) driver).executeScript("prompt('enter text...');"));
}

使用Javascript可以创建POST请求,设置所需的参数和HTTP标头,然后提交.

and with Javascript you can create a POST request, set the required parameters and HTTP headers, and submit it.

var xhr = new XMLHttpRequest();
// false as 3rd argument will forces synchronous processing
xhr.open('POST', 'http://httpbin.org/post', false);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('login=test&password=test');
alert(xhr.response);

如果您需要传递给硒响应文本,请使用return this.responseTextreturn this.response代替alert(this.responseText)并分配 executeScript()(针对Java)到变量.

If you need to pass over to selenium the response text then instead of alert(this.responseText) use return this.responseText or return this.response and assign the result of execute_script (python) (executeScript() for java) to a variable.

这是python的完整示例:

Here is a full example for python:

from selenium import webdriver

driver = webdriver.Chrome()

js = '''var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://httpbin.org/post', false);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

xhr.send('login=test&password=test');
return xhr.response;'''

result = driver.execute_script(js);

result将包含您的JavaScript的返回值,前提是js代码是同步的.将false设置为xhr.open(..)的第三个参数将强制请求同步.将第三个arg设置为true或将其省略将使请求异步,这将需要类似xhr.onload = function({alert(this.responseText);};的内容来处理结果.

result will contain the return value of your JavaScript provided that the js code is synchronous. Setting false as the third argument to xhr.open(..) forces the request to be synchronous. Setting the 3rd arg to true or omitting it will make the request asynchronous which will require something like xhr.onload = function({alert(this.responseText);}; to handle the result.

注意:如果需要将字符串参数传递给javascript,请确保始终使用json.dumps(myString)对其进行转义,否则,当字符串包含单引号或双引号或其他棘手的字符时,js就会中断.

NOTE: If you need to pass string arguments to the javascript make sure you always escape them using json.dumps(myString) or otherwise your js will break when the string contains single or double quotes or other tricky characters.

这篇关于在Selenium中发出POST请求而不填写表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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