Which loop should I use to remove all data entries within a table using Selenium?

332 views Asked by At

I need to code out a loop to remove all entries within the table. To remove the entries, I need to use Selenium to 'tick' the delete checkbox and click on 'save'. However, every entry has a unique 'checkbox' name. The first entry will be called delete0, the second entry is called delete1 and so on so forth. I am looking to create a loop that will check for the presence of 'delete0' and if so, delete the entry until there are no more entries.

I tried using a while loop but am not sure what conditions I should implement as I am new to Python. This is the loop that I've tried:

while driver.find_element_by_class_name("delete0") exist:
    driver.find_element_by_xpath("//input[@type='checkbox']").click()
    driver.find_element_by_id("save-tcp").click()

and this is the code for the website:

<form action="TCPFile" method="POST" enctype="multipart/form-data">
                                <div class="form-bottom">
                                    <div class="table-responsive">
                                        <table class="table table-bordered text-center" id="table_tcpfile">
                                            <thead class="log">
                                                <tr>
                                                    <th class="text-center">Index <i class="fa fa-info-circle" data-toggle="tooltip" data-placement="top" title="This index number is used to map to the corresponding index number on the receiver TCP to file settings page."></i></th>
                                                    <th class="text-center">Port <i class="fa fa-info-circle" data-toggle="tooltip" data-placement="top" title="" data-original-title="Enter port of TCP server."></i></th>  
                                                    <th class="text-center">Delete</th>
                                                </tr>
                                            </thead>
                                            <tbody>

                                                    <tr>             
                                                        <td><input type="text" class="Map disabled-input" name="index0" value="0"></td>
                                                        <td><input type="text" class="Map disabled-input" name="port0" value="12333"></td>          
                                                        <td><input type="checkbox" class="" name="delete0"></td>
                                                    </tr>

                                                    <tr>             
                                                        <td><input type="text" class="Map disabled-input" name="index1" value="1"></td>
                                                        <td><input type="text" class="Map disabled-input" name="port1" value="12345"></td>          
                                                        <td><input type="checkbox" class="" name="delete1"></td>
                                                    </tr>

                                                    <tr>             
                                                        <td><input type="text" class="Map disabled-input" name="index2" value="2"></td>
                                                        <td><input type="text" class="Map disabled-input" name="port2" value="13555"></td>          
                                                        <td><input type="checkbox" class="" name="delete2"></td>
                                                    </tr>

                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <div class="button-box col-lg-12">
                                    <button type="submit" class="col-2 form-control btn btn-primary " id="save-tcp">Save</button>
                                </div>
                            </form>

The expected result is to remove all data entries.

2

There are 2 answers

0
Andersson On BEST ANSWER

You can match input fields by partial @name value:

for item in driver.find_elements_by_xpath("//input[starts-with(@name, 'delete')]"):
    item.click()
driver.find_element_by_id("save-tcp").click()
4
Mate Mrše On

Try something like this (sorry if the syntax is incorrect, not a Python expert):

checkboxes = driver.find_elements_by_css_selector("input[type='checkbox']")
while checkboxes:
   driver.find_element_by_css_selector("//input[@type='checkbox']").click() 
   checkboxes = driver.find_elements_by_css_selector("input[type='checkbox']")