How to execute javascript function in python with selenium

I have a function called "checkdata (code)" in javascript that, as you can see, takes an argument called "code" to run and returns a 15-char string.

So, I discovered (and tested) how to call functions without arguments in javascript, but my problem is that when I call checkdata (code), I always get the return value of "none". This is what I am doing so far:

wd = webdriver.Firefox() wd.get('My Webpage') a = wd.execute_script("return checkdata()", code) //Code is a local variable //from my python script print a 

I am doing this since I read it in the informal documentation on selenium and here: link

But, as I said, I just do not print.

How can I call a function that passes this parameter?

+7
source share
2 answers

Build string

 a = wd.execute_script("return checkdata('" + code + "');") 
+7
source

Instead of creating a string (this means that you will need to escape from your quotes correctly), try the following:

 a = wd.execute_script("return checkdata(arguments[0])", code) 
+4
source

All Articles