【问题标题】:Search context in robot framework在机器人框架中搜索上下文
【发布时间】:2021-04-13 18:35:13
【问题描述】:

在 selenium Java 中,我们可以通过 findElement(locator1).findElement(locator2) 找到一个元素 我们也可以做到
DefaultElementLocatorFactory locatorFactory = new DefaultElementLocatorFactory(getElement()); PageFactory.initElements(locatorFactory, this); Q1。我们如何在机器人框架中实现相同的目标? Q2。我们如何在 python 中实现同样的功能?

【问题讨论】:

    标签: java python selenium selenium-webdriver robotframework


    【解决方案1】:

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

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

    创建关键字

      Get child element
    

    它需要两个参数 parent(Webelement) 和 child(Xpath) 在 parent 中搜索 child。

      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}
    

    【讨论】:

    • 抱歉回来晚了。但是我尝试了以下错误${child}= Execute Javascript return window.document.evaluate( arguments[1], arguments[0], null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue; ARGUMENTS //section[contains(@class, 'tabContent active oneConsoleTab')]//section[contains(@class, 'tabContent active oneConsoleTab')] //h1/div[contains(text(), 'Case')] JavascriptException:消息:javascript 错误:无法在“文档”上执行“评估”:参数 2 不是“节点”类型。
    猜你喜欢
    • 2020-04-02
    • 2016-05-19
    • 2018-05-06
    • 2013-02-13
    • 2018-04-25
    • 2013-12-19
    • 2011-03-09
    • 2018-07-31
    • 2018-08-21
    相关资源
    最近更新 更多