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

Question:

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?

Asked By: Alexander

||

Answers:

you can access value of tag or any element of html via .text or .getText()

Answered By: rocknrold

Since you are using find_elements_by_tag_name() which returns list of elements.
Iterate that list and check element.text contains task then print text of that element.

p_id = driver.find_elements_by_tag_name("script")
for id in p_id:
    if 'task' in id.text:
        print(id.text)
Answered By: KunduK

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.

Answered By: 0buz

#Use Xpath instead:

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("url_goes_here")

p_id = driver.find_element(By.XPATH,"ADDXPATH")

p_id.get_attribute('outerHTML')
Answered By: Nasir