Issue
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.
Answered By - 0buz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.