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