I'm exploring Kenneth Reitz's requests_html and trying to submit a form of a JS Rendered Webpage using Jquery. I'm not sure how to do it but, here is my attempt:
from requests_html import HTMLSession
url = "https://example.com"
session = HTMLSession()
r = session.get(url)
r.html.render()
script = """
() => {
$("#some_input_field").val("Some value");
$("#submit_button").click();
}
"""
r.html.render(script=script, reload=False)
But, the value is not getting set on the input field & it isn't submitting the form...
Is there any way to simulate button click or, form submit via xhr
in requests_html?
For example: If we use selenium we can simulate button click pretty easily by typing:
element.click()
Ok, The following code is working in my case:
from requests_html import HTMLSession
url = "https://example.com"
session = HTMLSession()
r = session.get(url)
r.html.render()
script = """
() => {
if ( jQuery.isReady ) {
$("#some_input_field").val("Some value");
$("#submit_button").click();
}
}
"""
r.html.render(script=script, reload=False)
EDIT: A better approach should be:
from requests_html import HTMLSession
url = "https://example.com"
session = HTMLSession()
r = session.get(url)
r.html.render()
script = """
() => {
$(document).ready(function() {
$("#some_input_field").val("Some value");
$("#submit_button").click();
})
}
"""
r.html.render(script=script, reload=False)