Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access text field using selenium when main identifiers are auto-generated

I have a HTML table which outputs some data on each row and for each row includes a text field. I have a separate dataframe with values I need to input into the relevant row but I can't figure out how to get the correct text input element as the name isn't unique.

I can get the element which contains 123456/1 so I can find the row containing the PartA I want but I can't figure out how to get the text input field id="VALUE.ENTER.SYSTEM.1-XY that then corresponds to that row. .XY is a number value that changes depending on the number of rows of data and I can't assume that a particular value of XY corresponds to the value I want to look for.

I don't know the full @value for the row and the values listed on the page is limited to 50 at a time so just because I don't find it once doesn't mean it won't appear on a later page. I need to find the 1st row in the table, extract a value from it and then if I have a match in another dataframe, if I do I want to add a value to the text box at the end of the row before moving on to the next row and repeating the extract, compare, submit steps until I run out of rows

Any reliable way to locate the text input field in python that has

title ="EnterValueHere"

My Code

for row in rows:
    RowData = row.find_elements_by_tag_name("input")
    for cell in RowData:
        #Get the ID in question
        if "/" in cell.get_attribute("value"):
            TextToSplit =cell.get_attribute("value")
            PartA,PartB= str(TextToSplit).split("/")
            print(PartA)

Sample Table

  <tr>
                <td class="tablesaw-cell-persist">
                  <input type="hidden" name="UNIQUE_ID.SYSTEM.01" value="12">
                  <input type="hidden" name="HEADER_ID.SYSTEM.01" value="">
                  123456/1<input type="hidden" name="CODE.SYSTEM.01" value="123456/1">
                  <span id="ANCHOR.SYSTEM.01"></span>
                </td>
                <td class="tablesaw-cell-persist">
                  BLOGGS JOE<input type="hidden" name="NAME.SYSTEM.01" value="JOE BLOGGS">
                </td>
                <td class="tablesaw-cell-persist">
                  1<input type="hidden" name="ATTEMPT.SYSTEM.01" value="1">
                </td>

                <td>
                  <input type="hidden" name="PRODUCTID.DUM_ASSESSMENT.MENSYS.1-12" value="XY1234+1">
                  <input type="hidden" name="SUS_CODE.DUM_ASSESSMENT.MENSYS.1-12" value="">

                  <div class="sv-input-group-table"><div class="sv-input-group"><span class="sv-input-group-addon"><span class="mme-input-group">

                   N<input type="hidden" name="RTS_CODE.DUM_ASSESSMENT.MENSYS.1-12" value="N">
                  </span></span><input type="text" class="sv-form-control sv-form-control-xs" name="VALUE.ENTER.SYSTEM.1-12" id="VALUE.ENTER.SYSTEM.1-12" title="EnterValueHere" value=""></div></div>
                  <span class="fbk-input-group fbk-select"></span>
                </td>


</tr>
<tr>
                <td class="tablesaw-cell-persist">
                  <input type="hidden" name="UNIQUE_ID.SYSTEM.02" value="12">
                  <input type="hidden" name="HEADER_ID.SYSTEM.02" value="">
                  123456/1<input type="hidden" name="CODE.SYSTEM.02" value="234456/1">
                  <span id="ANCHOR.SYSTEM.02"></span>
                </td>
                <td class="tablesaw-cell-persist">
                  BLOGGS JOE<input type="hidden" name="NAME.SYSTEM.02" value="J BLOGGS">
                </td>
                <td class="tablesaw-cell-persist">
                  1<input type="hidden" name="ATTEMPT.SYSTEM.02" value="1">
                </td>

                <td>
                  <input type="hidden" name="PRODUCTID.DUM_ASSESSMENT.MENSYS.1-02" value="XY1234+2">
                  <input type="hidden" name="SUS_CODE.DUM_ASSESSMENT.MENSYS.1-02" value="">

                  <div class="sv-input-group-table"><div class="sv-input-group"><span class="sv-input-group-addon"><span class="mme-input-group">

                   N<input type="hidden" name="RTS_CODE.DUM_ASSESSMENT.MENSYS.1-02" value="N">
                  </span></span><input type="text" class="sv-form-control sv-form-control-xs" name="VALUE.ENTER.SYSTEM.1-02" id="VALUE.ENTER.SYSTEM.1-02" title="EnterValueHere" value=""></div></div>
                  <span class="fbk-input-group fbk-select"></span>
                </td>


 </tr>
 <tr>
                <td class="tablesaw-cell-persist">
                  <input type="hidden" name="UNIQUE_ID.SYSTEM.03" value="12">
                  <input type="hidden" name="HEADER_ID.SYSTEM.03" value="">
                  123456/1<input type="hidden" name="CODE.SYSTEM.03" value="56789/1">
                  <span id="ANCHOR.SYSTEM.02"></span>
                </td>
                <td class="tablesaw-cell-persist">
                  BLOGGS JOE<input type="hidden" name="NAME.SYSTEM.03" value="JM BLOGGS">
                </td>
                <td class="tablesaw-cell-persist">
                  1<input type="hidden" name="ATTEMPT.SYSTEM.03" value="1">
                </td>

                <td>
                  <input type="hidden" name="PRODUCTID.DUM_ASSESSMENT.MENSYS.1-03" value="XY1234+2">
                  <input type="hidden" name="SUS_CODE.DUM_ASSESSMENT.MENSYS.1-03" value="">

                  <div class="sv-input-group-table"><div class="sv-input-group"><span class="sv-input-group-addon"><span class="mme-input-group">

                   N<input type="hidden" name="RTS_CODE.DUM_ASSESSMENT.MENSYS.1-02" value="N">
                  </span></span><input type="text" class="sv-form-control sv-form-control-xs" name="VALUE.ENTER.SYSTEM.1-03" id="VALUE.ENTER.SYSTEM.1-03" title="EnterValueHere" value=""></div></div>
                  <span class="fbk-input-group fbk-select"></span>
                </td>


 </tr>
like image 262
mobcdi Avatar asked May 20 '18 21:05

mobcdi


Video Answer


1 Answers

So first you need to get the TR for your code 123456/1. The xpath would be

//tr[td//input[@name="CODE.SYSTEM.XY"][@value=123456/1]

So above is to get the tr element which contains your input element. Now you want the input to be located inside this tr, which can be done by extending the XPath to

//tr[td//input[@name="CODE.SYSTEM.XY"][@value=123456/1]/td//input[@title='EnterValueHere']

Updated-1: 30th May 2018

You should use

//tr[td//input[starts-with(@name, 'CODE.SYSTEM.')][starts-with(@value, '123456/')]/td//input[@title='EnterValueHere']

the first section says find a tr anywhere in the document then [] condition says which has an element td and child input field anywhere hwre which as attribute name starting with CODE.... and value starting 12345/ once the tr is found find another td inside that which has an input with title entervaluehere

like image 197
Tarun Lalwani Avatar answered Sep 29 '22 08:09

Tarun Lalwani