Capture AJAX response with selenium and python

后端 未结 3 1683
眼角桃花
眼角桃花 2020-12-05 14:51

I click on a link in Firefox, the webpage sends a request using javascript, then the server sends some sort of response which includes a website address. So this new website

3条回答
  •  难免孤独
    2020-12-05 15:32

    I once intercepted some ajax calls injecting javascript to the page using selenium. The bad side of the history is that selenium could sometimes be, let's say "fragile". So for no reason I got selenium exceptions while doing this injection.

    Anyway, my idea was intercept the XHR calls, and set its response to a new dom element created by me that I could manipulate from selenium. In the condition for the interception you can even use the url that made the request in order to just intercept the one that you actually want (self._url)

    btw, I got the idea from intercept all ajax calls?

    Maybe this helps.

    browser.execute_script("""
    (function(XHR) {
      "use strict";
    
      var element = document.createElement('div');
      element.id = "interceptedResponse";
      element.appendChild(document.createTextNode(""));
      document.body.appendChild(element);
    
      var open = XHR.prototype.open;
      var send = XHR.prototype.send;
    
      XHR.prototype.open = function(method, url, async, user, pass) {
        this._url = url; // want to track the url requested
        open.call(this, method, url, async, user, pass);
      };
    
      XHR.prototype.send = function(data) {
        var self = this;
        var oldOnReadyStateChange;
        var url = this._url;
    
        function onReadyStateChange() {
          if(self.status === 200 && self.readyState == 4 /* complete */) {
            document.getElementById("interceptedResponse").innerHTML +=
              '{"data":' + self.responseText + '}*****';
          }
          if(oldOnReadyStateChange) {
            oldOnReadyStateChange();
          }
        }
    
        if(this.addEventListener) {
          this.addEventListener("readystatechange", onReadyStateChange,
            false);
        } else {
          oldOnReadyStateChange = this.onreadystatechange;
          this.onreadystatechange = onReadyStateChange;
        }
        send.call(this, data);
      }
    })(XMLHttpRequest);
    """)
    

提交回复
热议问题