Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a Javascript function in python with selenium

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

So, I found out (and tested) how to call no-argument functions in javascript, but my problem is that when I call checkdata(code), I always get a 'none' return value. This is what I'm 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'm making this, since I read it on an unofficial selenium documentation and here: link

But, as I said before, I just keep getting none printed.

How can I call my function passing that parameter?

like image 248
Jose_Sunstrider Avatar asked Dec 30 '12 05:12

Jose_Sunstrider


2 Answers

Build the string

a = wd.execute_script("return checkdata('" + code + "');")
like image 156
epascarello Avatar answered Sep 20 '22 09:09

epascarello


Rather than building a string (which means you'd have to escape your quotes properly), try this:

a = wd.execute_script("return checkdata(arguments[0])", code)
like image 45
user839397 Avatar answered Sep 22 '22 09:09

user839397