14

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?

Community
  • 1
  • 1
Jose_Sunstrider
  • 323
  • 1
  • 3
  • 17
  • See also this [similar question](http://stackoverflow.com/questions/7794087/running-javascript-in-selenium-using-python). – franklin Mar 01 '16 at 16:26

2 Answers2

14

Build the string

a = wd.execute_script("return checkdata('" + code + "');")
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Ah, got it working thanks to you!, here is what my code looks like: `a = local.execute_script(' return checkdata(\"'+code+'\")') print a` Thanks for the help! – Jose_Sunstrider Dec 30 '12 at 05:48
10

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)
user839397
  • 119
  • 1
  • 3