Passing a variable through Java Script Executor in selenium Webdriver

4,946 views
Skip to first unread message

visha...@gmail.com

unread,
May 20, 2014, 5:55:24 AM5/20/14
to webd...@googlegroups.com
Hi 

I have a date picker field in the webpage. I am sending the values in the field with the help of java script executor in my java code. I would like to make that field data driven and pass the values through variables. I have tried in the following way:

String date1=values[2]+"/"+values[3]+"/"+values[4]+" 09:30am";
String date2=values[5]+"/"+values[6]+"/"+values[7]+" 06:30pm";
executor.executeScript("document.getElementById('rangedate__Datepicker1').value = date1");
executor.executeScript("document.getElementById('year-rangedate__Datepicker1').value = values[4]");
executor.executeScript("document.getElementById('month-rangedate__Datepicker1').value = month(values[3]");
executor.executeScript("document.getElementById('day-rangedate__Datepicker1').value = values[2]");


but i get the following error, " org.openqa.selenium.WebDriverException: JavaScript error". Can you please tell me how can i make send the values through variables in the above commands?

Sean Adkinson

unread,
May 20, 2014, 11:31:29 AM5/20/14
to webd...@googlegroups.com
You have a error in your javascript:

    month(values[3]");

Looks like you are missing a closing parenthesis.

-Sean

visha...@gmail.com

unread,
May 21, 2014, 2:16:10 AM5/21/14
to webd...@googlegroups.com
Hi Sean

I corrected the missing parenthesis, but still i get the same error at this line:
executor.executeScript("document.getElementById('rangedate__Datepicker1').value = date1");

Sean Adkinson

unread,
May 21, 2014, 11:53:52 AM5/21/14
to webd...@googlegroups.com
What you are doing looks pretty sound, so try this: take the javascript that you believe is running, substituting the variables that WebDriver would substitute, and run it directly in your browser's javascript console.  WebDriver doesn't do anything special when running javascript, so if you are getting a javascript error, you should see it when running the commands by themselves, and perhaps get a better idea of what is going wrong.

-Sean



--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.



--
Sean Adkinson
Senior Software Engineer - Jama Software
  

visha...@gmail.com

unread,
May 22, 2014, 2:59:45 AM5/22/14
to webd...@googlegroups.com
Hi Sean

I believe that problem is while passing a value through a Java variable

String date1=values[2]+"/"+values[3]+"/"+values[4]+" 09:30am"
executor.executeScript("document.getElementById('rangedate__Datepicker1').value = date1");

Fore example in the above statement date1 is the java variable that we are passing through a javascript call and which believe is causing the error, because when give a static value in the place of date1 in the javascript call, the script runs without any error.? 

Is there any way through which i can make my java script call data driven?

Phanindra Chowdary Chunduri

unread,
May 22, 2014, 3:11:57 AM5/22/14
to webd...@googlegroups.com
Hi,


Your code should look as below.

executor.executeScript("document.getElementById('rangedate__Datepicker1').value ='" + rangeDateVar + "';");
executor.executeScript("document.getElementByIdyear-rangedate__Datepicker1').value ='" + yearDateVar + "';");
executor.executeScript("document.getElementById('month-rangedate__Datepicker1').value ='" + monthDateVar + "';");
executor.executeScript("document.getElementById('day-rangedate__Datepicker1').value ='" + dayDateVar + "';");


Now write whatever logic u want to initialize the variables. You want to read values[] you can read. Do whatever you want just initialize below variables to correct values and use the above code. It will work.

String rangeDateVar = "value you want to pass" , yearDateVar="value you want to pass"  , monthDateVar ="value you want to pass" , dayDateVar ="value you want to pass" ;

Sean Adkinson

unread,
May 22, 2014, 12:12:44 PM5/22/14
to webd...@googlegroups.com
The scope of your Java variables is not shared with JavaScript, so you can't reference them the way you are trying here.

Pass a variable number of arguments to executeScript and reference them via the arguments array in javascript:

String date1=values[2]+"/"+values[3]+"/"+values[4]+" 09:30am"
executor.executeScript("document.getElementById('rangedate__Datepicker1').value = arguments[0]", date1);

This is the safest way to pass variables to your javascript.  A less-safe but perhaps more-readable alternative is string concatenation:

String date1=values[2]+"/"+values[3]+"/"+values[4]+" 09:30am"
executor.executeScript("document.getElementById('rangedate__Datepicker1').value = '" + date1 + "'");

In this case, you need to make sure to quote your strings the way I have done above.

-Sean



visha...@gmail.com

unread,
May 26, 2014, 1:39:28 AM5/26/14
to webd...@googlegroups.com
Thank you Sean, I was able to execute the script by the changes you had suggested.
Reply all
Reply to author
Forward
0 new messages