1

I am trying to web scrape some information from a website local storage using selenium webdriver. I am using python 3.8.5 I want to return that JavaScript's output value to a python variable.

number = driver.execute_script("setTimeout(function(){ localStorage.getItem('value'); }, 1500);")

But when I print the variable number the result is None for some reason. Any ideas on what I should do?

4 Answers 4

2

You have to return from the JS script:

print(driver.execute_script('return localStorage.getItem("value");'))
Sign up to request clarification or add additional context in comments.

Comments

1

This will navigate to this page where you asked this question and return the localStorage item "se:fkey" using javascript:

from selenium import webdriver

driver = webdriver.Chrome(executable_path=r'C:\\Path\\To\\Your\\chromedriver.exe')
driver.get('https://stackoverflow.com/questions/63283026/how-to-get-the-return-value-of-a-javascript-function-to-a-python-variable')

a_returned = driver.execute_script("""
a_function = function(){
console.log(localStorage.getItem("se:fkey"));
return localStorage.getItem("se:fkey");
};
return a_function();

""")

print("a_returned:", a_returned)

Comments

0

If you want to pass any value from client-side (javascript) to backend (python) you can use ajax for data sending here: https://www.w3schools.com/jquery/jquery_ajax_get_post.asp

But the best way is to fetch the required data directly through python that would be fast enough. There are many packages/libraries like selenium, requests, beautiful soap with them you can do that.

Comments

-1

You can use this:

number = driver.execute_script("setTimeout(function(){ return localStorage.getItem('value'); }, 1500);")

1 Comment

a verbal explanation is often helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.