简体   繁体   English

Splinter:如何将参数传递给execute_script?

[英]Splinter: How to pass argument to execute_script?

I need to execute a javascript function on the page, by passing it an array of strings. 我需要在页面上执行javascript函数,方法是将字符串数组传递给它。 I want to avoid having to call browser.execute_script number of times. 我想避免不得不多次调用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)

Dump python list to JSON and use string formatting : 将python列表转储为JSON并使用字符串格式

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: 这是它产生的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}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM