【问题标题】:How to use the xpath to find a element by the parent? c# selenium如何使用 xpath 通过父元素查找元素? c# 硒
【发布时间】:2021-05-07 00:07:24
【问题描述】:

在我正在测试的网页中,它们是两个几乎相同的模态对话框,用于enabledisable网页中的一个参数,实际上基本上是相同的代码,我需要使用带有文本“是”的按钮,两个模态之间的唯一区别是一个 id,这是模态对话框的代码:

<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable" tabindex="-1" role="dialog" aria-labelledby="ui-dialog-title-dialogEnable" style="display: none; z-index: 1006; outline: 0px; height: auto; width: 300px; top: 244px; left: 228px;">
    <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
        <span class="ui-dialog-title" id="ui-dialog-title-dialogEnable">Delete</span>
            <a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button">
                <span class="ui-icon ui-icon-closethick">close</span>
            </a>
    </div>
    <div id="dialogEnable" class="dialog ui-dialog-content ui-widget-content" scrolltop="0" scrollleft="0" style="width: auto; min-height: 63px; height: auto;">
        <p>Are you sure you want to delete the selected record?</p>
    </div>
    <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
        <div class="ui-dialog-buttonset">
            <button type="button" id="btnYes" class="button primary ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
                <span class="ui-button-text">Yes</span>
            </button>
        </div>
        <a class="cancel-dialog" href="#">No</a>
    </div>
</div>

这是另一个模态的代码:

<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable" tabindex="-1" role="dialog" aria-labelledby="ui-dialog-title-dialogDisable" style="display: none; z-index: 1006; outline: 0px; height: auto; width: 300px; top: 244px; left: 228px;">
    <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
        <span class="ui-dialog-title" id="ui-dialog-title-dialogDisable">Recover</span>
            <a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button">
                <span class="ui-icon ui-icon-closethick">close</span>
            </a>
    </div>
    <div id="dialogDisable" class="dialog ui-dialog-content ui-widget-content" scrolltop="0" scrollleft="0" style="width: auto; min-height: 63px; height: auto;">
    <p>Are you sure you can recover the selected record?</p>
    </div>
    <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
        <div class="ui-dialog-buttonset">
            <button type="button" id="btnYes" class="button primary ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
                <span class="ui-button-text">Yes</span>
            </button>
        </div>
        <a class="cancel-dialog" href="#">No</a>
    </div>
</div>

这是区分两种模式的 id:

<div id="dialogEnable" class="dialog ui-dialog-content ui-widget-content" scrolltop="0" scrollleft="0" style="width: auto; min-height: 63px; height: auto;">
        <p>Are you sure you want to delete the selected record?</p>
    </div>
<div id="dialogDisable" class="dialog ui-dialog-content ui-widget-content" scrolltop="0" scrollleft="0" style="width: auto; min-height: 63px; height: auto;">
    <p>Are you sure you can recover the selected record?</p>
    </div>

如果我在禁用模式对话框中使用 de 按钮“是”中的 id,我没有问题,但我不能为启用模式使用相同的 id,所以我想我可以使用按钮中的 id 是的,如果我使用 id="dialogDisable" 作为父母,问题是我不知道该怎么做,所以我希望你们能帮助我

【问题讨论】:

  • 你想找什么,请提及
  • 对不起,我已经编辑了问题,我希望这些图片有助于理解上下文
  • 不,但不是因为它错了,但这不是我要找的东西,来自 Dazed 的答案正是我要找的,无论如何,谢谢!很抱歉没有正确写出问题大声笑

标签: c# visual-studio selenium


【解决方案1】:

我想你要找的是这个吗?

//div[@id='dialogEnable']/following::span[1][text()='Yes'] - returns 1


//div[@id='dialogDisable']/following::span[1][text()='Yes'] - returns 1

【讨论】:

  • 其实是的,很抱歉,如果我的问题很难理解,但我对答案还有另一个问题,“-returns 1”是做什么的?我从不使用 xpath,至少不使用 selenium,我使用路径但没有返回部分并且它可以工作,但我只想知道以防我将来需要它
  • 不要忽略这一点。我假设您的 xpath 返回 2 个“是”值,因此 selenium 无法识别要单击的值。我只是说这些 xpath 调用为每个模式返回了 1 个“是”。
  • 哦,好的,我明白了,无论如何,谢谢你,你真的帮了我很多:) 祝你有美好的一天!
【解决方案2】:

我不确定你的问题,如果你的问题是如何找到 div 元素,那么你可以使用以下任何一种方法。

//div[@id="dialogDisable" or @id="dialogEnable"]
//div[contains(@id,"dialog")]

【讨论】:

    猜你喜欢
    • 2020-03-25
    • 2016-07-28
    • 2020-05-03
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 2016-03-12
    • 2017-09-14
    相关资源
    最近更新 更多