dimanche 4 avril 2021

Selenium Chrome Headless giving incomplete local storage

I am trying to scrape a JWT token from a site to then use in my program.

When using selenium chrome without headless mode, I'm able to grab the JWT cookie: Example of valid local storage

However, when using headless, I get the following incomplete data: Example of incomplete local storage

def get_dex_tool_cookie():
    from selenium.webdriver.chrome.options import Options
    from selenium import webdriver
    from chromedriver_py import binary_path

    import time
    import pprint

    options = Options()
    #options.add_argument("--headless")

    experimentalFlags = ['same-site-by-default-cookies@1', 'cookies-without-same-site-must-be-secure@2']
    chromeLocalStatePrefs = {'browser.enabled_labs_experiments': experimentalFlags}
    options.add_experimental_option('localState', chromeLocalStatePrefs)
    
    driver = webdriver.Chrome(options=options, executable_path=binary_path)
    driver.delete_all_cookies()

    driver.get('https://www.dextools.io/app/')
    pprint.pprint(driver.execute_script("return localStorage;"))

    time.sleep(5)

    # fetching website again as the website should set the cookie after the previous visit
    driver.get('https://www.dextools.io/app/')
    pprint.pprint(driver.execute_script("return localStorage;"))
    
    jwt = None
    try:
        x = driver.execute_script("return window.localStorage;")

        jwt = x['t']
        return jwt
    except KeyError as e:
        return None
    finally:
        driver.close()
        driver.quit()

print(get_dex_tool_cookie())

Any assistance would be apprecited.




Aucun commentaire:

Enregistrer un commentaire