Robot Framework - Selenium2Library - Input text into field according to the text before it

Goralight picture Goralight · Nov 18, 2016 · Viewed 9.7k times · Source

Due to every ID being randomly generated after each refresh, I am having to use other identifiers. I basically want Robot to input text into a field next to my indicated identifier.

https://vgy.me/3YBIgl.png

I want text to be inputted to the user: field and using the "user:" as the locator to work with. It works with xpaths, but I would rather use another method which wont be so brittle.

Here is the HTML used to generate the form

<tbody id="m8DPe" class="z-rows">
<tr id="m8DPf" style="background:#FFFFFF;" class="z-row">
    <td id="m8DPg-chdextr" style="background:#FFFFFF;text-align:left;" class="z-row-inner">
        <div id="m8DPg-cell" class="z-row-content">
            <span id="m8DPg" class="z-label">User:</span>
        </div>
    </td>
    <td id="m8DPh-chdextr" style="background:#FFFFFF;text-align:left;" class="z-row-inner">
        <div id="m8DPh-cell" class="z-row-content">
            <input id="m8DPh" class="z-textbox" value="" type="text" name="j_username">
        </div>
    </td>
</tr>
<tr id="m8DPi" style="background:#FFFFFF;" class="z-row z-grid-odd">
    <td id="m8DPj-chdextr" style="background:#FFFFFF;text-align:left;" class="z-row-inner">
        <div id="m8DPj-cell" class="z-row-content">
        <span id="m8DPj" class="z-label">Password:</span>
        </div>
    </td>
    <td id="m8DPk-chdextr" style="background:#FFFFFF;text-align:left;" class="z-row-inner">
        <div id="m8DPk-cell" class="z-row-content">
            <input id="m8DPk" class="z-textbox" value="" type="password" name="j_password">
        </div>
    </td>
</tr>

I know it would be something similar to this:

Input Text    //tr[contains(text(), 'Example') and ...]    ${USERNAME}

But I honestly do not know. Is there somewhere I could read up on this?

I am extremely new to Robot Framework. Sorry for the noobness.

Answer

Todor Minakov picture Todor Minakov · Mar 3, 2017

Looking at the sample, the name attribute for the input doesn'the look randomly generated. If that's really so, you could use the most trivial Selenium locator strategy by name, e.g.:

Input Text    name=j_username    ${USERNAME}

If that's not the case, this can be accomplished through an xpath:

//tr[//span[text()="User:"]/td//input

That reads (right-to-left for clearness, though it's evaluated LTR): return the input, which is inside a td (a cell), which itself is a direct child of a tr (a table row), having a span with that text (exact match here).

Thus, the locator will find the row having cell with "User:" in it, and will return the input in it.