简体   繁体   中英

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:

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

<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

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[@id="dialogDisable" or @id="dialogEnable"]
//div[contains(@id,"dialog")]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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