我需要在页面上执行javascript函数,方法是传递一个字符串数组。我想避免不得不多次拨打browser.execute_script
。
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)
答案 0 :(得分:1)
将python列表转储到JSON
并使用字符串格式:
import json
json_array = json.dumps(list_of_strings)
result = browser.execute_script("function findTexts(%s){//code here}" % json_array)
以下是它生成的js代码:
>>> 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}'