使用JavaScript访问通过自定义定位器策略定位的元素 [英] Accessing elements located by custom locator strategy with Javascript
本文介绍了使用JavaScript访问通过自定义定位器策略定位的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下问题:
- 我有一个隐藏的复选框,还有一层用于使它变得漂亮又有光泽
- 我可以通过访问其ID轻松更改其值,但是我还需要使用我的自定义定位器(使用xpath(必须保持可变))来访问此元素
所以,这是我的脚本:
Custom Select Checkbox id=my_checkbox #that works fine
Custom Select Checkbox customLocatorStrat #that doesn't work at all
*** Keywords ***
Custom Select Checkbox
[Arguments] ${locator} ${timeout}=${global_timeout}
Execute Javascript document.getElementById("${resultLocator}").checked = true;
OR
${el}.checked = true; #need to get the ${el} variable if the ${locator is not an id}
My Custom Locator
[Arguments] ${criteria} ${tag} ${constraints}
... #assembling xpath
${el}= Get Webelements xpath=${path}
[Return] ${el}
定位器策略没有问题-可以正常工作.我只需要在自定义选择复选框"关键字中使用它/强制使用它即可.我需要获取$ {el}变量,并且考虑到有不止一个Custom Locator关键字直接调用它对我不起作用.您知道如何执行此操作吗?预先非常感谢.
There is no issue with the locator strategy - it workes fine. I only need to use it/force it in my Custom Select Checkbox keyword. I need to get the ${el} variable and considering there's more than one Custom Locator keyword calling it directly will not work for me. Any idea how to do this please? Many thanks in advance.
推荐答案
selenium2library中的自定义定位器需要通过Add Location Strategy
Custom locators in the selenium2library require activation through the Add Location Strategy
[Doc]. This seems to me to be the missing from your example.
*** Test Cases ***
Test Case
Add Location Strategy custom Custom Locator Strategy
Page Should Contain Element custom=my_id
*** Keywords ***
Custom Locator Strategy
[Arguments] ${browser} ${criteria} ${tag} ${constraints}
${retVal}= Execute Javascript return
window.document.getElementById('${criteria}');
[Return] ${retVal}
这篇关于使用JavaScript访问通过自定义定位器策略定位的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文