机器人框架中的搜索上下文

2024-10-02 20:30:20 发布

您现在位置:Python中文网/ 问答频道 /正文

在selenium Java中,我们可以通过findElement(locator1).findElement(locator2)找到一个元素,其中包含一个元素 我们也可以做
DefaultElementLocatorFactory locatorFactory = new DefaultElementLocatorFactory(getElement()); PageFactory.initElements(locatorFactory, this); 问题1。我们如何在机器人框架中实现同样的事情? 问题2。我们如何在python中实现同样的功能


Tags: 框架元素newselenium机器人javathisfindelement
1条回答
网友
1楼 · 发布于 2024-10-02 20:30:20

https://github.com/robotframework/SeleniumLibrary/issues/672

没有内置函数,但您可以使用:

创建关键字

  Get child element

需要两个参数parent(Webelement)和child(Xpath)来搜索父级中的子级

  Get child element    ${parent}    ./h3

假设父xpath为://ul[@id="top_menu"],上述代码将搜索等价xpath//ul[@id="top_menu"]/./h3。注意,taht上下文是父上下文而不是根上下文

所以这将等同于xpath

*** Test Cases ***
Google Search
    [Tags]    you    probably    do    not    have    this    many    tags    in    real    life
    Wait Until Element Is Visible  CSS=[id="introduction-container"]
    ${parent}=    Get webelement    xpath=//*[@id="introduction-container"]
    ${result}=    Get child element    ${parent}    ./h3   
        
    
*** Keywords ***
Get child element 
    [Arguments]    ${parent}   ${child} 
    ${child}=    Execute Javascript    return window.document.evaluate( arguments[1], arguments[0], null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;     ARGUMENTS    ${parent}    ${child}
    [Return]    ${child}

相关问题 更多 >