有没有办法在Robot中将WebElement传递给javascript?
有没有办法在Robot中将WebElement传递给javascript?我的剧本:有没有办法在Robot中将WebElement传递给javascript?,javascript,python,robotframework,selenium2library,Javascript,Python,Robotframework,Selenium2library,有没有办法在Robot中将WebElement传递给javascript?我的剧本: ${el} = Get Webelement ${locator} Execute Javascript ${el}.checked = true; #not sure how to approach this bit 我不能使用document.getElementById(“随便什么”),因为在某些情况下,只使用自定义定位器而不使用ID 非常感谢你的帮助 否,不能将web元素传递给
${el} = Get Webelement ${locator}
Execute Javascript ${el}.checked = true; #not sure how to approach this bit
我不能使用document.getElementById(“随便什么”),因为在某些情况下,只使用自定义定位器而不使用ID
非常感谢你的帮助 否,不能将web元素传递给javascript。它是一个python对象 但是,如果要调用对象上的方法,则不需要使用javascript。例如,要检查元素是否被选中,可以执行
${el.is_selected()}
这里记录了可用的元素方法:否,您不能将web元素传递给javascript。它是一个python对象 但是,如果要调用对象上的方法,则不需要使用javascript。例如,要检查元素是否被选中,可以执行
${el.is_selected()}
这里记录了可用的元素方法:这是不可能的,但是您可以尝试的是,使用关键字为您定位的元素提供一个id,然后使用
文档在javascript中检索您的元素。getElementById
编辑:如果指定Id不是一个选项,那么您可以查看上面提到的关键字如何将Id指定给元素,并使用相同的技巧实现您自己的关键字,但要选中复选框。大概是这样的:
element = self._element_find(locator, True, True)
self._current_browser().execute_script("arguments[0].checked=true;", 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()}
因此,是的,我通过上述组合得到了解决方案:
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()}
您可以使用arguments关键字将参数传递给Executer,您可以尝试打印${child}并查看
您可以使用arguments关键字将参数传递给Executer,您可以尝试打印${child}并查看Perfect,这似乎可以用于检查是否选中了复选框-是否有一种方法可以使用类似的方法选择它?或者,是否有一种方法可以在不单击复选框的情况下选择该复选框?因为该元素不可见。基本上,我正在寻找一种替换方法:document.getElementById(“whatever”).checked=true;在Javascript中,应选中此答案中的pure selenium调用或
${checked}=Run关键字和返回状态复选框${locator}
Perfect,这似乎可以用于检查复选框是否选中-是否有方法使用类似的方法选择它?或者可能,有没有办法不点击复选框就选中它?因为该元素不可见。基本上,我正在寻找一种替换方法:document.getElementById(“whatever”).checked=true;在Javascript中,应选中此答案中的pure selenium调用或${checked}=Run关键字和返回状态复选框${locator}
对此不确定,因为屏幕上的流程的其他部分可能依赖于元素ID。然后,也许您可以编写自己的关键字,使用与我提到的关键字相同的技术(将Id分配给元素)。查看Selenium2库中的源代码,了解它如何将web元素传递给javascript(参数[0]部分)。不幸的是,我认为执行Javascript
不允许您按原样传递这些参数。不确定这一点,因为屏幕上的流程的其他部分可能依赖于元素ID。然后,也许您可以使用与我提到的关键字相同的技术编写自己的关键字(将ID分配给元素)。查看Selenium2库中的源代码,了解它如何将web元素传递给javascript(参数[0]部分)。不幸的是,我认为executejavascript
不允许按原样传递这些参数。