multiple seleniumwire requests in same script

824 views Asked by At

I'm running a Python scripts that does the following:

  • Open webpage
  • Login to game with login form
  • Wait for page to load (get Json response of Ajax request)
  • Perform a couple of clicks
  • Click on a button which triggers an ajax request
  • Need to read json response of the Ajax request. < This is where my problem is
  • Write to file

My problem is that I an unable to catch the second round of Ajax requests and I couldn't find any solution when Googling around.

My code looks like that:

import sys
import json
import time

from seleniumwire import webdriver  # Import from seleniumwire
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary(r'C:\\Program Files\\Mozilla Firefox\\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)

driver.get(myURL)

# Login code here

loginButton = driver.find_element_by_id('login_Login')
loginButton.click();

for request in driver.requests:     # Initial page loading
    if "login_check" in request.path:
        if request.response:
            response_body = json.loads(request.response.body)
            for key, value in response_body.items():
                if key == 'success':
                    if value is False:
                        # do some stuff and write to file
                        #driver.quit()
                        break
                    else:
                        defaultContent_switched = driver.switch_to.default_content()

                        try:
                            # do some stuff and write to file
                            
                        except TimeoutException:
                        
                            canvas = driver.find_element_by_tag_name('canvas')
                            canvas.click();
                            #click on a few elements        

                            for request in driver.requests:
                                if "/game/json" in request.path:
                                    print(request.path);    # At this point I only see the requests of the initial load of the page
                                    if request.response:
                                        # Need to do stuff here...
                                    break
            break
        break

print(json.dumps(output))
driver.quit()

How can I start listening to the network again after the click on the canvas?

1

There are 1 answers

1
Dilip Meghwal On

You can use below Ajax load function whenever there is page load or some content load to wait till the Ajax load completed.

#Wait webpage to fully load 
def ajaxwait():
    for i in range(1, 30):
        x = driver.execute_script("return (window.jQuery != null) && jQuery.active")
        time.sleep(1)
        if x == 0:
            print("All Ajax element loaded successfully")
            break