I need to execute a javascript function on the page, by passing it an array of strings. I want to avoid having to call browser.execute_script
number of times.
list_of_strings = ['a','b','c']
#js code runs through all the strings
result = browser.execute_script("function findTexts(textList){//code here}", list_of_strings)
Dump python list to JSON
and use string formatting:
import json
json_array = json.dumps(list_of_strings)
result = browser.execute_script("function findTexts(%s){//code here}" % json_array)
Here's what js code does it produce:
>>> import json
>>> list_of_strings = ['a','b','c']
>>> json_array = json.dumps(list_of_strings)
>>> "function findTexts(%s){//code here}" % json_array
'function findTexts(["a", "b", "c"]){//code here}'