javascript - selenium driver.execute_script 中的 Python var

标签 javascript python arguments robotframework keyword

我正在为 robotframework 创建一些自定义关键字,但遇到了一个问题。

如果我给 javascript 一个参数,我就会得到以下关键字,例如:

from robot.api.deco import keyword
from robot.libraries.BuiltIn import BuiltIn
from SeleniumLibrary import SeleniumLibrary

class roboJSlib:

    @keyword('Checkbox select')
    def check(self, arg):
        driver = BuiltIn().get_library_instance('SeleniumLibrary')._current_browser()
        driver.execute_script("document.getElementById('preventivo_privacy_accetto_informative').click()")

我希望脚本将“arg”作为 id,它在 robotframework 中定义为参数。

有人知道这是否可行吗?

最佳答案

您可以将参数传递给 execute_script 方法并使用 arguments[x] 语法在 JavaScript 中引用它们,因此在您的情况下它将是:

from robot.api.deco import keyword
from robot.libraries.BuiltIn import BuiltIn
from SeleniumLibrary import SeleniumLibrary

class roboJSlib:

    @keyword('Checkbox select')
    def check(self, arg):
        driver = BuiltIn().get_library_instance('SeleniumLibrary')._current_browser()
        driver.execute_script("document.getElementById(arguments[0]).click()", arg)

关于javascript - selenium driver.execute_script 中的 Python var,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56871819/

相关文章:

javascript - 火狐插件。如何真正下载图像/文件?

使用 Ajax 将 Javascript 变量传递给 PHP

Python 单元测试类型错误

python - django中定义模型的顺序

c++ - 为什么带引号的字符串在 std::string 之前匹配 bool 方法签名?

python - 在 python 中用函数参数命名的列表

javascript - 如何使用链接在输入框之间导航?

javascript - 互斥提交

找不到 Python 3.4 命令

c - C 结构中函数的参数