简体   繁体   English

如何使用 xpath 找到父元素? c# selenium

[英]How to use the xpath to find a element by the parent? c# selenium

In the web page i'm doing the test for, they're two modal dialogs that are practilally the same, the're used to enable and disable a parameter in the web page, in fact is basically the same code, i need to use the button with the text "Yes", the only difference between the two modals is one id, this is the code of the modal dialogs:在我正在测试的web页面中,它们是两个几乎相同的模态对话框,用于启用禁用web页面中的参数,实际上基本上是相同的代码,我需要使用带有文本“是”的按钮,两个模态之间的唯一区别是一个 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>

And this is the code from the other modal:这是另一个模式的代码:

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

This is the id that difference both modals:这是区分两种模式的 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>

If i'm use the id from de button "Yes" from the disable modal dialog, i don't have problem, but i can't use the same id for the enable modal, so i think i can use the id from the button Yes if i use id="dialogDisable" as a parent, the problem is that i don't know how to do that, so i hope you guy can help me如果我使用禁用模式对话框中的 de 按钮“是”的 id,我没有问题,但我不能为启用模式使用相同的 id,所以我想我可以使用来自按钮 是的,如果我使用id="dialogDisable"作为父母,问题是我不知道该怎么做,所以我希望你们能帮助我

I think what you are looking for is this?我想你要找的是这个吗?

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


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

I am not sure of your question, if your questions how to find the div element then you can use any of following methods.我不确定您的问题,如果您的问题是如何找到 div 元素,那么您可以使用以下任何一种方法。

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

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

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