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?
Answers:
you can access value of tag
or any element
of html via .text
or .getText()
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)
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.
#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')
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?
you can access value of tag
or any element
of html via .text
or .getText()
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)
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.
#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')