Making a POST request in Selenium without filling a form?
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 useexecute_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.
I don't think that's possible using Selenium. There isn't a way to create a POST request out of nothing using a web browser, and Selenium works by manipulating web browsers. I'd suggest you use a HTTP library to send the POST request instead, and run that alongside your Selenium tests. (What language/testing framework are you using?)