Reputation: 833
Is there a way to pass a WebElement to javascript in Robot? My script:
${el} = Get Webelement ${locator}
Execute Javascript ${el}.checked = true; #not sure how to approach this bit
I cannot use document.getElementById("whatever") as in some certain cases there's no ID used but custom locators.
Many thanks for your help!
Upvotes: 1
Views: 1119
Reputation: 19989
${child}= Execute Javascript return arguments; ARGUMENTS ${child} ${parent}
you can pass arguments to Executer using ARGUMENTS keyword, you can try print ${child} and see
Upvotes: 0
Reputation: 833
So yes, I've got the solution by combination of above:
Select Checkbox
[Arguments] ${locator} ${timeout}=${global_timeout}
${tempId} = Generate Random String 8
Wait Until Page Contains Element locator=${locator} timeout=${timeout}
${origId} = Get Element Attribute ${locator}@id
Assign Id To Element locator=${locator} id=${tempId}
Execute Javascript document.getElementById("${tempId}").checked = true;
${locator} = Replace String string=${locator} search_for=${origId} replace_with=${tempId}
Assign Id To Element locator=${locator} id=${origId}
Unselect Checkbox
[Arguments] ${locator} ${timeout}=${global_timeout}
${tempId} = Generate Random String 8
Wait Until Page Contains Element locator=${locator} timeout=${timeout}
${origId} = Get Element Attribute ${locator}@id
Assign Id To Element locator=${locator} id=${tempId}
Execute Javascript document.getElementById("${tempId}").checked = false;
${locator} = Replace String string=${locator} search_for=${origId} replace_with=${tempId}
Assign Id To Element locator=${locator} id=${origId}
Checkbox Should Be Selected
[Arguments] ${locator} ${timeout}=${global_timeout}
Wait Until Page Contains Element locator=${locator} timeout=${timeout}
${el} = Get Webelement ${locator}
Should Be True ${el.is_selected()}
Checkbox Should Not Be Selected
[Arguments] ${locator} ${timeout}=${global_timeout}
Wait Until Page Contains Element locator=${locator} timeout=${timeout}
${el} = Get Webelement ${locator}
Should Not Be True ${el.is_selected()}
Upvotes: 3
Reputation: 2473
It's not possible, but what you could try is, giving an id to your located element, using the keyword Assign Id To Element and then retrieve your element in the javascript with document.getElementById
Edit: If assigning an Id isn't an option, you could look into how the keyword mentioned above assigns the id to the element and implement your own keyword using the same trick, but to check your checkbox. Something like this:
element = self._element_find(locator, True, True)
self._current_browser().execute_script("arguments[0].checked=true;", element)
Upvotes: 4
Reputation: 386362
No, you cannot pass the web element to javascript. It is a python object.
However, you don't need to use javascript if you want to call a method on the object. For example, to check if the element is selected you can do ${el.is_selected()}
The available element methods are documented here: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement
Upvotes: 2