Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Splitter:如何传递参数以执行_脚本?_Javascript_Python_Splinter - Fatal编程技术网

Javascript Splitter:如何传递参数以执行_脚本?

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并使用字符串格式: 下面

我需要在页面上执行一个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
并使用字符串格式:

下面是它产生的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}'