1

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?

2 Answers 2

1

You could pass arguments to execute_script method and refer to them in JavaScript using arguments[x] syntax, so in your case it would be:

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.