如何在python代码中插入get_元素_count
我必须将以下robot framework关键字转换为python代码。你能帮帮我吗 Robot框架示例关键字:如何在python代码中插入get_元素_count,python,robotframework,Python,Robotframework,我必须将以下robot framework关键字转换为python代码。你能帮帮我吗 Robot框架示例关键字: *variables* ${locator} xpath=(//div[@it="testID"])[2] *keyword* sample keyword ${count} Get Element Count ${locator} 在python文件中。我使用了以下命令 from robot.libraries.BuiltIn import BuiltI
*variables*
${locator} xpath=(//div[@it="testID"])[2]
*keyword*
sample keyword ${count} Get Element Count ${locator}
在python文件中。我使用了以下命令
from robot.libraries.BuiltIn import BuiltIn
def _helper keyword(locator):
count=BuiltIn.run_keyword(get_element_count,locator)
当我执行它时,我收到以下错误消息:
namererror:name'get\u element\u count'未定义。
方法get\u element\u count
实际上来自SeleniumLibrary;如果它的调用像这样失败,可能意味着您没有在运行函数的上下文中导入它(例如,调用此函数的套件中没有Library SeleniumLibrary
,或者它导入的任何资源)
一旦你解决了这个问题,有一个稍微好一点的方法来调用它的方法-你可以使用并直接调用它的关键字,而不是通过run\u keyword
:
se_lib = BuiltIn().get_library_instance('SeleniumLibrary')
cnt = se_lib.get_element_count(locator)
不过,为了让它正常工作,显然需要导入库,以便获得一个实例
注意:不要用“count”作为变量名——这是您刚刚覆盖的python内置函数——隐藏了下游的bug;) 获取元素计数的方法实际上来自硒库;如果它的调用像这样失败,可能意味着您没有在运行函数的上下文中导入它(例如,调用此函数的套件中没有
Library SeleniumLibrary
,或者它导入的任何资源)
一旦你解决了这个问题,有一个稍微好一点的方法来调用它的方法-你可以使用并直接调用它的关键字,而不是通过run\u keyword
:
se_lib = BuiltIn().get_library_instance('SeleniumLibrary')
cnt = se_lib.get_element_count(locator)
不过,为了让它正常工作,显然需要导入库,以便获得一个实例
注意:不要用“count”作为变量名——这是您刚刚覆盖的python内置函数——隐藏了下游的bug;) 首先,不需要xpath中的
xpath=
,只需使用:
*variables*
${locator} | (//div[@it="testID"])[2]
其次,get_element_count是一个来自SeleniumLibrary的关键字,而不是内置的。因此,您需要导入SeleniumLibrary来调用它:
def get_element_count(locator):
context = BuiltIn().get_library_instance('SeleniumLibrary')
return context.get_element_count(locator)
首先,不需要xpath中的
xpath=
,只需使用:
*variables*
${locator} | (//div[@it="testID"])[2]
其次,get_element_count是一个来自SeleniumLibrary的关键字,而不是内置的。因此,您需要导入SeleniumLibrary来调用它:
def get_element_count(locator):
context = BuiltIn().get_library_instance('SeleniumLibrary')
return context.get_element_count(locator)