Warm tip: This article is reproduced from stackoverflow.com, please click
javascript python arguments robotframework keyword

Python var inside selenium driver.execute_script

发布于 2020-03-27 10:20:15

I'm creating some custom keywords for robotframework and I'm stuck on a problem.

I've got the following keyword that is working if I give javascript an argument, for example:

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()")

I want the script to get "arg" as id, which is defined in robotframework as an argument.

Does anyone know if this is possible?

Questioner
Andrea
Viewed
55
Andrea 2019-07-04 00:29

found the solution:

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

Thanks to everyone.