Search code examples
javascriptpythonseleniumgoogle-chromevar

Getting the value of a script's "var" using Selenium with Python


from selenium import webdriver

driver = webdriver.Chrome()
driver.get("url_goes_here")

p_id = driver.find_elements_by_tag_name("script")

This procures me the script I need. I don't need to execute it, as it's already executed and running upon initial page load. It contains a variable named "task". How do I access its value with Selenium?


Solution

  • The regex module re can help you with that:

    import re
    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get("url_goes_here")
    
    p_id = driver.find_elements_by_tag_name("script")
    
    for script in p_id:
        innerHTML=script.get_property('innerHTML')
        task=re.search('var task = (.*);',innerHTML)
        if task is not None:
            print(task.group(1))
    

    What this does is look through the innerHTML of each script and, from the defined search pattern ('var task = (.*);'), capture the matching string group ((.*)). Print out the group if a match is found.