elchueko
elchueko

Reputation: 439

Python Selenium WebDriver - Possible to monitor XHR AJAX Calls?

I am using Python with Selenium WebDriver to browse through website.

Now I have the problem that I want to monitor the XHR AJAX calls, thrown on the current site.

For example: I am on a specific page and the python selenium script clicks on a button on this site. This website button calls an AJAX request within this site. Is it possible to monitor this XHR AJAX request and get it in my python script to handle the called AJAX URL?

Thanks!

UPDATE: I exactly want to do sth like this (but in python obviously)

https://dmitripavlutin.com/catch-the-xmlhttp-request-in-plain-javascript/

Upvotes: 3

Views: 3204

Answers (1)

Arnon Axelrod
Arnon Axelrod

Reputation: 1672

You can use browser.execute_script to catch the calls as explained in the link that you mentioned. In addition, start a fake website using Django on a separate thread. And in the JavaScript handler (sendReplacement), replace the url with the one of your django server. In that server you will receive the AJAX call and be able to examine it. You may be able to implement a simpler solution without the django server by simply monitor the calls directly from the JavaScript snippet and make it return the value you want directly. But if you need to monitor many calls and perform more complex examination is the requests, the former solution is more powerful.

Upvotes: 1

Related Questions