Well, the title says it all... It is possible to perform an XmlHttpRequest from Selenium/Webdriver and then render the output of that requests in a browser instance ? If so, can you enlight me please ?
Asked
Active
Viewed 1,908 times
3
-
Could you please describe the use case and motivation behind the question? – alecxe Jul 03 '15 at 18:13
-
In tumblr, when you search for posts, the page has infinite scrolling. It issues an XHR to fetch more posts... having the page loaded, the xhr should automatically update the current page ??? – Jeflopo Jul 03 '15 at 18:36
-
You can approach it by simulating that infinite scrolling with python+selenium. – alecxe Jul 03 '15 at 18:37
-
I know that I can scroll the page, but I would like to do it using http requests... – Jeflopo Jul 03 '15 at 18:38
-
Then, you just don't need selenium. Use `requests` package, or even better - the tumblr api, https://github.com/tumblr/pytumblr. – alecxe Jul 03 '15 at 18:39
3 Answers
1
One option would be to make the XHR request using, for instance, requests
, save the response to a file and get()
it with selenium webdriver instance (How to use selenium webdriver on local (on my pc) webpage instead of locate somwhere on www?).
0
Selenium is really designed to be an external control system for a web browser. I don't think of it as being the source of test data, itself. There are other unit-testing frameworks which are designed for this purpose, but I see Selenium's intended purpose to be different.

Mike Robinson
- 8,490
- 5
- 28
- 41
0
I think you want something like this:
fileupload_url = 'https://this-site.com/upload'
filedata = open('example.txt').read()
ajax_request = '''
var xhr = new XMLHttpRequest();
xhr.setRequestHeader('Content-type', 'application/octet-stream'); // text/plain
xhr.onload = function(){{ document.body.innerHTML += '<div>Passed!</div>';
alert (xhr.responseText); }} // success case
xhr.onerror = function(){{ document.body.innerHTML += '<div>Failed!</div>';
alert (xhr.responseText); }} // failure case
xhr.open ('POST', {}, true);
xhr.send ({});
'''.format(fileupload_url, filedata)
driver.execute_script(ajax_request)

nmz787
- 1,960
- 1
- 21
- 35