python-2.7 - 在机器人中找不到名为“Get Child WebElement”的关键字
问题描述
我想从当前页面获取所有子元素,因为我正在使用下面的代码
${childElements} = Get Child WebElement xpath=//*
上面的代码抛出一个错误 No keyword with name 'Get Child WebElement'
有没有其他图书馆可以工作?
解决方案
正如您所发现的,这些关键字尚未出现在已发布的 SeleniumLibrary 版本中。
如果您正在处理一个元素或只是有一个字符串定位器,那么您可以使用自定义 SeleniumLibrary 扩展中@yash 的答案中提到的SelenniumLibrary Pull Request #702中建议的代码。
在下面的示例中,有两个自定义 Robot Framework 关键字可以使用 #702 中提到的方法为您提供关键字:
- 获取子 Web 元素
- 获取父 Web 元素
在下面的示例中,它显示您可以使用常规定位器id=main
或 WebElement来查找父级和子级${children[0]}
。这是由python 对象的 python 方法处理的find_element(s)
。WebElement
注意:出于安全原因和可读性,将关键字从Evaluate
更改为。Call Method
例子.机器人
*** Settings ***
Library SeleniumLibrary
Suite Teardown Close All Browsers
*** Test Cases ***
Get Children Then Parent
Open Browser http://www.google.com headlesschrome
${children} Get Child Webelements id=main
${parent} Get Parent Webelement ${children[0]}
${id} Get Element Attribute ${parent} id
Should Be Equal ${id} main
*** Keywords ***
Get Child Webelements
[Arguments] ${locator}
${element} Get WebElement ${locator}
${children} Call Method
... ${element}
... find_elements
... by=xpath value=child::*
[Return] ${children}
Get Parent Webelement
[Arguments] ${locator}
${element} Get WebElement ${locator}
${parent} Call Method
... ${element}
... find_element
... by=xpath value=parent::*
[Return] ${parent}
推荐阅读
- javascript - 如何在页面第一部分隐藏元素
- c# - “/”应用程序中的 System.Resources.MissingManifestResourceException 服务器错误
- python - Python 中的 Strip 方法并不能全部清除
- session - Internet Explorer 等待用户操作 - 后台不刷新
- python - 在 tk.Entry 中显示灰色的默认文本
- angular - 在打字稿中使生命周期钩子异步(Angular,Ionic)
- python - 如何将代码编程为运行特定时间并在此之后退出?
- java - 检测点击了哪个按钮
- java - 无法解析带有嵌套 CDATA 部分的 XML
- powershell - 使用 PowerShell 和写字板转换为 PDF