wkeeling / selenium-wire

Extends Selenium's Python bindings to give you the ability to inspect requests made by the browser.
MIT License
1.9k stars 254 forks source link

'del driver.requests' doesn't seem to work #568

Closed helloHKTK closed 2 years ago

helloHKTK commented 2 years ago

I call "get_request()" many times in the main method, and each time I use 'del driver.requests', but it seems that 'del driver.requests' does not work, that is, when requesting 'http://github.com', it returns the result of the previous 'http://bing.com'. the code is as follows: ` from loguru import logger from seleniumwire import webdriver # Import from seleniumwire from selenium.webdriver.chrome.service import Service

def get_request(domain_url): if len(driver.requests) > 0: del driver.requests # clear previously captured requests and HAR entries logger.info(f"driver_requests_len={len(driver.requests)}") driver.get(domain_url)

for request in driver.requests:
    if request.response:
        url = request.url
        response_headers = request.response.headers['Content-Type'],
        if response_headers[0] and 'javascript' in response_headers[0]:
            logger.info(
                f"domain_url={domain_url}, url={url}")
logger.info("#" * 30)

path = '...' s = Service(path)

option = webdriver.ChromeOptions() option.add_argument('--headless')

driver = webdriver.Chrome(service=s, options=option) get_request('http://bing.com') get_request('http://github.com')

driver.quit() `

wkeeling commented 2 years ago

del driver.requests will delete requests that are stored at the moment it is called, but the issue can boil down to timing.

When driver.get() finishes for bing.com it is possible that background requests are still in-flight. These requests are thus received by Selenium Wire after del driver.requests has been called - and so they can be seen in the subsequent log for github.com.

To be sure that all bing.com requests have completed, you'd need to put a time.sleep() in between the twoget_request() calls.

helloHKTK commented 2 years ago

您好:    很高兴收到您的来信,我会尽快阅读并回复您!祝您工作顺利、生活愉快!

kadiryaren commented 1 year ago

嗨,你找到答案了吗?我还在等。

helloHKTK commented 1 year ago

您好:    很高兴收到您的来信,我会尽快阅读并回复您!祝您工作顺利、生活愉快!