Warm tip: This article is reproduced from serverfault.com, please click

selenium-查找是否存在下一个同级

(selenium - Find if next sibling exist)

发布于 2020-12-08 20:24:31

我尝试编写一些机器人测试用例,但被卡住了。我需要从页面中提取一些链接。链接存储在<li>标记内,该标记也位于内<ul>问题是,提取第一个链接后,我需要转到下一个链接<li><li>下一个同级兄弟),但是如何找到没有错误或应用程序停止的下一个同级兄弟。我知道,我可以使用Element Should Contain关键字,但是如果其中不包含关键字,则会出现错误。
我想做这样的事情:

if next sibling exist  do something
else go do something else

html应该看起来像这样。

<ul id="top_menu" role="menubar" class="">
    <li class="menu_accordion_section" id="cat_1">
        <a href="some-exampe"> </a>
    </li>
    <li class="menu_accordion_section" id="cat_2">
        <a href="some-exampe"> </a>
    </li>
</ul>
Questioner
tibi
Viewed
0
PDHide 2021-01-08 10:29:53

有直接消息,你可以使用执行脚本来创建变量。

所以我创建了一个关键字

  Validate child element exists

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

  Validate child element exists    ${parent}    ./h3

假设将父xpath设置为as //ul[@id="top_menu"],则上面的代码将搜索等价的xpath//ul[@id="top_menu"]/./h3注意:上下文是父级而不是root

所以这将等同于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}=    Validate child element exists    ${parent}    ./h3   
    Should Be True     ${result}

    
*** Keywords ***
Validate child element exists 
    [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}