有没有办法将WebElement传递给Robot中的javascript?我的剧本:
${el} = Get Webelement ${locator}
Execute Javascript ${el}.checked = true; #not sure how to approach this bit
我不能使用document.getElementById(“whatever”),因为在某些情况下,没有使用ID但是自定义定位器。
非常感谢你的帮助!
答案 0 :(得分:4)
这是不可能的,但您可以尝试的是,使用关键字Assign Id To Element为您找到的元素提供ID,然后使用document.getElementById
编辑:如果分配ID不是一个选项,您可以查看上面提到的关键字如何将id分配给元素并使用相同的技巧实现您自己的关键字,但要检查您的复选框。像这样:
element = self._element_find(locator, True, True)
self._current_browser().execute_script("arguments[0].checked=true;", element)
答案 1 :(得分:3)
所以是的,我通过以上的组合获得了解决方案:
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()}
答案 2 :(得分:2)
不,你不能将web元素传递给javascript。它是一个python对象。
但是,如果要在对象上调用方法,则不需要使用javascript。例如,要检查元素是否已选中,您可以执行${el.is_selected()}
此处记录了可用的元素方法:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement
答案 3 :(得分:0)
${child}= Execute Javascript return arguments; ARGUMENTS ${child} ${parent}
您可以使用ARGUMENTS关键字将参数传递给Executer,可以尝试打印$ {child}并查看