Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python代码中插入get_元素_count_Python_Robotframework - Fatal编程技术网

如何在python代码中插入get_元素_count

如何在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

我必须将以下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 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)