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

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

问题描述

我有一个应用程序 A 应该处理使用 POST 方法提交的表单.发起请求的实际表单位于完全独立的应用程序 B 中.我正在使用 Selenium 测试应用程序 A,并且我喜欢编写表单提交处理的测试用例.

如何做到这一点?这可以在硒中完成吗?应用程序 A 没有可以发起此请求的表单.

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

解决方案

使用 selenium,您可以执行任意 Javascript,包括 以编程方式提交表单.

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

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

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

//POST 请求的 Javascript 示例var xhr = new XMLHttpRequest();//false 作为第三个参数将强制同步处理xhr.open('POST', 'http://httpbin.org/post', false);xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');xhr.send('登录=测试&密码=测试');警报(xhr.response);

<块引用>

在现代前沿浏览器中,您还可以使用 fetch().

如果您需要将响应文本传递给 selenium,则使用 return this.responseTextreturn this.response 而不是 alert(this.responseText) 并将 execute_script(或 execute_async_script) (如果使用python).对于 Java,这将是 executeScript()executeAsyncScript() 相应.

这是一个完整的python示例:

from selenium import webdriver驱动程序 = 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('登录=测试&密码=测试');返回 xhr.response;'''结果 = driver.execute_script(js);

result 将包含 JavaScript 的返回值,前提是 js 代码是同步的.将 false 设置为 xhr.open(..) 的第三个参数会强制请求同步.将第三个参数设置为 true 或省略它将使请求异步.

<块引用>

❗️ 如果您正在调用 异步 js 代码,请确保使用的是 execute_script 你使用 execute_async_script 否则调用将不会返回任何内容!

<块引用>

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

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.

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

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

解决方案

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

Simplest JS execution with Selenium Java:

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

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

// Javascript example of a POST request
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);

In modern bleeding edge browsers you can also use fetch().

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 to a variable the result of execute_script (or execute_async_script) (if using python). For java that will be executeScript() or executeAsyncScript() correspondingly.

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 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.

❗️ If you are calling asynchronous js code then make sure that instead of execute_script you use execute_async_script or otherwise the call won't return anything!

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天全站免登陆