简体   繁体   English

在机器人中找不到名称为“ Get Child WebElement”的关键字

[英]No keyword with name 'Get Child WebElement' found In Robot

I want to get all child elements from Current page, for that I am using below code 我想从当前页面获取所有子元素,为此我正在使用以下代码

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

The above code throws an error No keyword with name 'Get Child WebElement' 上面的代码抛出一个错误No关键字,名称为“ Get Child WebElement”

Is there any other library for this to work? 还有其他的库可以工作吗?

As you have figured out that these keywords are not yet in the published SeleniumLibrary versions. 如您所知,这些关键字尚未在已发布的SeleniumLibrary版本中。

In the event that you are processing an element or simply have a string locator then you can use the proposed code in SelenniumLibrary Pull Request #702 mentioned in the answer from @yash in a custom SeleniumLibrary extension. 如果您正在处理元素或仅具有字符串定位符,则可以在自定义SeleniumLibrary扩展名中@yash答案中提到的SelenniumLibrary Pull Request #702中使用建议的代码。

In the below example there are two custom Robot Framework keywords to give you the keywords using the approach mentioned in #702: 在以下示例中,有两个自定义的Robot Framework关键字,可使用#702中提到的方法为您提供关键字:

  1. Get Child Webelements 获取子Webelements
  2. Get Parent Webelement 获取父级Webelement

In the below example it shows that you can look for the parent and child using a regular locator id=main or a WebElement ${children[0]} . 在下面的示例中,它显示您可以使用常规定位符id=main或WebElement ${children[0]}查找父级和子级。 This is handled by the python method find_element(s) of the WebElement python object. 这由WebElement python对象的python方法find_element(s)处理。

Note: changed the keyword from Evaluate to Call Method for security reasons and readability. 注意:出于安全原因和易读性,将关键字从“ Evaluate更改为“ Call Method ”。

example.robot example.robot

*** 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}  

There is PR #702 raised for the same. 有相同的PR#702 There are some ongoing discussion going on around that. 关于这一点,正在进行一些讨论。 As there is no any ETA/Conclusion on this yet. 由于目前尚无任何ETA /结论。 Meanwhile you can control all things from your identifier. 同时,您可以通过标识符控制所有内容。

like if you want child element your identifier will be:- 例如,如果您想要子元素,则标识符将为:-
In case of css:- <<parent identifier > child >>
In case of xpath:- <<parent identifier/child>>

There is limitation of using this approach is you cant have 2 different selector type as parent/child. 使用此方法的局限性是您不能将2种不同的选择器类型作为父/子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM