Capture AJAX response with selenium and python

后端 未结 3 1671
眼角桃花
眼角桃花 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:24

    I've come up to this page when trying to catch XHR content based on AJAX requests. And I eventually found this package

    from seleniumwire import webdriver  # Import from seleniumwire
    # Create a new instance of the Firefox driver
    driver = webdriver.Firefox()
    
    # Go to the Google home page
    driver.get('https://www.google.com')
    
    # Access requests via the `requests` attribute
    for request in driver.requests:
        if request.response:
            print(
                request.url,
                request.response.status_code,
                request.response.headers['Content-Type']
            )
    

    this package allow to get the content response from any request, such as json :

    https://www.google.com/ 200 text/html; charset=UTF-8
    https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png 200 image/png
    https://consent.google.com/status?continue=https://www.google.com&pc=s×tamp=1531511954&gl=GB 204 text/html; charset=utf-8
    https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png 200 image/png
    https://ssl.gstatic.com/gb/images/i2_2ec824b0.png 200 image/png
    https://www.google.com/gen_204?s=webaft&t=aft&atyp=csi&ei=kgRJW7DBONKTlwTK77wQ&rt=wsrt.366,aft.58,prt.58 204 text/html; charset=UTF-8
    ..
    

提交回复
热议问题