0

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;
        }
Ross Patterson
  • 9,527
  • 33
  • 48
Shuffler
  • 173
  • 2
  • 11

1 Answers1

0

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']
Ross Patterson
  • 9,527
  • 33
  • 48
  • Yeah xpath is terrible and i know it but can't find a better one. The one you posted doensn't work because it's designed to pick wrong elements. TD's with span and actual data are siblings, not like u designed parent and child. Your comment though was helpfull enough for me to find the solution which is //tr[not(contains(@style,'display: none'))]/td/span[text()='Wykonaj akcję']//parent::*//following-sibling::*/select[not(contains(@disabled,'disabled'))] Thanks for your help! :) – Shuffler Feb 26 '13 at 15:10
  • I Still have problemw ith the following xpath. In firefox firePath the disabled element is not selected but in Selenium disabled value still can be changed. Any reason what could be the reason? – Shuffler Feb 27 '13 at 09:11
  • Im sorry i tested it again and i still doesnt work. It works fine with firefox firepath and visualizers but not as a selenium locator. I lways picks last element with desired span text not the one not hidden. – Shuffler Feb 27 '13 at 16:02