首页 > 解决方案 > 在机器人中找不到名为“Get Child WebElement”的关键字

问题描述

我想从当前页面获取所有子元素,因为我正在使用下面的代码

${childElements} =  Get Child WebElement    xpath=//*

上面的代码抛出一个错误 No keyword with name 'Get Child WebElement'

有没有其他图书馆可以工作?

标签: python-2.7robotframework

解决方案


正如您所发现的,这些关键字尚未出现在已发布的 SeleniumLibrary 版本中。

如果您正在处理一个元素或只是有一个字符串定位器,那么您可以使用自定义 SeleniumLibrary 扩展中@yash 的答案中提到的SelenniumLibrary Pull Request #702中建议的代码。

在下面的示例中,有两个自定义 Robot Framework 关键字可以使用 #702 中提到的方法为您提供关键字:

  1. 获取子 Web 元素
  2. 获取父 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}  

推荐阅读