Question

I have some question regarding how the selenium RC xpath. I have made some xpath string to match certain fields regardless of ids ( all are generated automatically).

My Xpath is matching an element in the table with certain conditions. Here is my html code

<table style="width:900px;">
    <tbody>
        <tr>
            <td colspan="2">
                <span class="header" id="ctl00_ContentPlaceHolder_ctl07">Nowy wniosek </span>
                <span class="description" id="ctl00_ContentPlaceHolder_ctl08"></span>
                <span></span>
            </td>
        </tr>
        <tr>
            <td width="34%" valign="top" bgcolor="#ffffc7">
                <span>Status wniosku</span>
                <span></span>
            </td>
            <td width="66%" bgcolor="#ffffc7">
                <input type="text" scriptattrib="scriptAttribstring" class="baseCtrl" id="ctl00_ContentPlaceHolder_1020" readonly="readonly" value="Nowy wniosek" name="ctl00$ContentPlaceHolder$1020"/>
                <span> </span>
                <span class="ctrlDescrpt">(Pole nie do edycji)</span>
                <span> </span>
                <span style="display:none;" class="validation" id="ctl00_ContentPlaceHolder_ctl21"></span>
                <span></span>
            </td>
        </tr>
        <tr>
            <td width="34%" valign="top">
                <span>Wykonaj akcję</span>
                <span></span>
            </td>
            <td width="66%">
                <select class="baseCtrl" id="ctl00_ContentPlaceHolder_1021" name="ctl00$ContentPlaceHolder$1021">
                    <option value="save">zapisz</option>
                    <option value="send">wyślij do przełożonego</option>
                    <option value="cancel">anuluj</option>
                </select>
                <span> </span>
                <span style="display:none;" class="validation" id="ctl00_ContentPlaceHolder_ctl32"></span>
                <span></span>
            </td>
        </tr>
        <tr style="display: none;">
            <td width="34%" valign="top" bgcolor="#ffffc7">
                <span>Wykonaj akcję</span>
                <span></span>
            </td>
            <td width="66%" bgcolor="#ffffc7">
                <select class="baseCtrl" id="ctl00_ContentPlaceHolder_1024" name="ctl00$ContentPlaceHolder$1024">
                    <option value="save">zapisz</option>
                    <option value="send2">prześlij dalej(ścieżka oddziały)</option>
                </select>
                <span> </span>
                <span style="display:none;" class="validation" id="ctl00_ContentPlaceHolder_ctl43"></span>
                <span></span>
            </td>
        </tr>
        <tr>
            <td width="34%" valign="top" bgcolor="#ffffc7">
                <span>Uwagi</span>
                <span></span>
            </td>
            <td width="66%" bgcolor="#ffffc7">
                <textarea scriptattrib="scriptAttribstring" class="baseCtrl" id="ctl00_ContentPlaceHolder_1022" readonly="readonly" cols="20" rows="5" name="ctl00$ContentPlaceHolder$1022"></textarea>
                <span> </span>
                <span style="display:none;" class="validation" id="ctl00_ContentPlaceHolder_ctl65"></span>
                <span></span>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <span></span>
            </td>
        </tr>
    </tbody>
</table>

Here is my Xpath

//span[text()='Wykonaj akcję']/parent::*/parent::*[not(contains(@style,'display: none'))]/child::*/following-sibling::*[not(contains(@style,'display: none'))]//select[not(contains(@disabled,'disabled'))]

Problem is the following. Tables are visible or not visible depending on person logged or some business logic behind. I've tried to make it so the TR element of the table can't contain style display: none;.

When i test the xpath in the firefox or some xpath visualizers everything seems to be fine.

My Selenium Code though returns the hidden element for some reason. (Returns last matching element and for him the hidden one is a match). I have no idea what the reason might be. Can anymore put some light into it?

Also this is my Selenium Code.

 var localizator = LocalizatorGenerators.SelectOptionLocator(labelName);
                var id = selenium.GetAttribute(localizator + "@id");
                selenium.SeleniumValidate(value, type, id);
                selenium.Select(string.Format("id={0}", id), value);



 public static string SelectOptionLocator(string labelName)
        {
            var xpath =
                String.Format(
                    "//span[text()='{0}']/parent::*/parent::*[not(contains(@style,'display: none;'))]/child::*/following-sibling::*[not(contains(@style,'display: none;'))]//select[not(contains(@disabled,'disabled'))]", labelName);
            return xpath;
        }
Était-ce utile?

La solution

That's a really horrible XPath locator. Your various combinations of parent::* and following-sibling::* make it extremely difficult to predict what this would match. But since what you're trying to identify is the select id="ctl00_ContentPlaceHolder_1021" without using its id value, try this:

//tr[not(contains(@style,'display: none'))]/td/[span[text()='Wykonaj akcję']]/td/select[@disabled != 'disabled']
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top