Javascript Splitter:如何传递参数以执行_脚本?
我需要在页面上执行一个javascript函数,通过向它传递一个字符串数组。我想避免调用Javascript Splitter:如何传递参数以执行_脚本?,javascript,python,splinter,Javascript,Python,Splinter,我需要在页面上执行一个javascript函数,通过向它传递一个字符串数组。我想避免调用浏览器。执行脚本的次数 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) 将python列表转储到JSON并使用字符串格式: 下面
浏览器。执行脚本的次数
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)
将python列表转储到JSON
并使用字符串格式:
下面是它产生的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}'
在javascript代码中,如何遍历列表?如果列表中的项目数不可预测怎么办?@KJW只需使用一个基本的javascript循环,请参见。我最终使用了browser.evaluate_脚本(“findTexts(%s);函数findTexts(list){//do something with list array}”)代码>结果是评估脚本
返回结果。
>>> 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}'