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

Can't get cookies from webdriver and also inject them #542

Closed badr-elmazaz closed 2 years ago

badr-elmazaz commented 2 years ago

I am trying to get cookies of a webpage and I am using selenium wire. So i made a simple workaround getting all the responses and extract the set cookie header, but when I geth them and I try to inject this cookies into another webdriver session it does not work, cookies are not set. This is the function to get the webdriver:

def _get_tor_browser():
    binary = FirefoxBinary(FIREFOX__BINARY)
    profile = FirefoxProfile(FIREFOX_PROFILE)
    opts = FirefoxOptions()
    profile.set_preference("security.cert_pinning.enforcement_level", 0)
    profile.set_preference("network.stricttransportsecurity.preloadlist", False)
    profile.set_preference("extensions.torbutton.local_tor_check", False)
    profile.set_preference("extensions.torbutton.use_nontor_proxy", True)
    profile.set_preference("browser.startup.homepage_override.mstone", "68.8.0")
    options = {
        'proxy': {
            'http': 'socks5h://127.0.0.1:9050',
            'https': 'socks5h://127.0.0.1:9050',
            'connection_timeout': 10
        }
    }
    profile.set_preference('network.proxy.type', 1)
    profile.set_preference('network.proxy.socks_host', '127.0.0.1')
    profile.set_preference('network.proxy.socks_port', 9050)
    profile.set_preference("network.proxy.socks_remote_dns", False)
    profile.update_preferences()
    tor_browser = webdriver.Firefox(firefox_binary=binary, firefox_profile=profile,
                                    seleniumwire_options=options,
                                    options=opts, service_log_path='./geckodriver2.log')
    tor_browser.implicitly_wait(10)
    return tor_browser

When it opens the browser an I try to go to a website like amazon.com and I try to use webdriver.get_cookies()it returns empty list but if I inspect that webpage and I go to console and I type document.cookie in the browser opened it returns that there are cookies. I also have problems injecting cookies using webdriver.add_cookie(cookie) it doesn t work. What am I wrong? Thanks previously for the help.

wkeeling commented 2 years ago

webdriver.get_cookies() and webdriver.add_cookie(cookie) are Selenium functions - they are not specific to Selenium Wire. Cookies are just HTTP headers, so to debug what is going on you could try examining the headers of the requests captured by Selenium Wire for amazon.com - e.g.

for request in driver.requests:
    if request.host.endswith('amazon.com'):
        print('Cookie: ', request.headers['Cookie'])
badr-elmazaz commented 2 years ago

Thanks wkeeling I did not know that those functions dont work in selenium wire. I fixed the problem using an interceptor that adds the Cookie header for each request. Good job for this fantastic library. Have a nice day.