0

I have a webpage with a HTML table with some columns and rows. The user can select a row from the table and click the delete button. This will delete the row from the html table. E.g. In the Name column (index 1) there is a value "Testa" The user can select the checkbox for the row where Name is "Testa" and delete it.

I would like to iterate over the rows and check "Testa" is not there. This way I can be sure the delete worked. My code is not iterating over the table.
My code snippet is:

def is_value_deleted_from_user_defined_field(self, name): # return true if the user defined name is null.  This method is to verify the user defined field has been deleted
    # Params name : The name of the user defined field e.g. AREACODE, Testa
    try:
        WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_edit_data_object_tab_details_tb_fields')))
        table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_edit_data_object_tab_details_tb_fields')))
        rows = table_id.find_elements(By.TAG_NAME, "tr")
        for row in rows:
            # Get the columns
            col_name = row.find_elements(By.TAG_NAME, "td")[1]  # This is the Name column
            print "col_name.text = "
            if (col_name.text != name):
                return True
        return False
    except NoSuchElementException, e:
        print "Element not found "
        print e
        self.save_screenshot("is_value_deleted_from_user_defined_field")
        return False

My code to call the method is:

self.assertTrue(data_objects_edit_page.is_value_deleted_from_user_defined_field("Testa"), "User Defiend field Testa has not been deleted")

The HTML snippet is:

    <table id="data_configuration_edit_data_object_tab_details_tb_fields" class="GFNQNVHJE border" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
    <thead aria-hidden="false">
        <colgroup>
            <tbody style="">
                <tr class="GFNQNVHCD GFNQNVHMD" __gwt_subrow="0" __gwt_row="0">
                    <td class="GFNQNVHBD GFNQNVHDD GFNQNVHED GFNQNVHLD GFNQNVHND">
                        <td class="GFNQNVHBD GFNQNVHDD GFNQNVHND">
                            <div __gwt_cell="cell-gwt-uid-407" style="outline-style:none;">
                                <input id="" class="" type="text" style="color: black;" value="AREACODE"/>
                            </div>
                        </td>
                        <td class="GFNQNVHBD GFNQNVHDD GFNQNVHND">
                            <div __gwt_cell="cell-gwt-uid-408" style="outline-style:none;">
                                <select tabindex="-1">
                                </div>
                            </td>
                            <td class="GFNQNVHBD GFNQNVHDD GFNQNVHOD GFNQNVHND">
                                <div __gwt_cell="cell-gwt-uid-409" style="outline-style:none;">
                                    <input id="" class="" type="text" style="color: black;" value="50"/>
                                </div>
                            </td>
                        </tr>
                        <tr class="GFNQNVHCE GFNQNVHJD" __gwt_subrow="0" __gwt_row="1">
                            <td class="GFNQNVHBD GFNQNVHDE GFNQNVHED GFNQNVHKD">
                                <div __gwt_cell="cell-gwt-uid-406" style="outline-style:none;">
                                    <input type="checkbox" tabindex="-1"/>
                                </div>
                            </td>
                            <td class="GFNQNVHBD GFNQNVHDE GFNQNVHKD">
                                <div __gwt_cell="cell-gwt-uid-407" style="outline-style:none;">
                                    <input id="" class="" type="text" style="color: black;" value="VSEQ"/>
                                </div>
                            </td>
                            <td class="GFNQNVHBD GFNQNVHDE GFNQNVHKD">
                                <div __gwt_cell="cell-gwt-uid-408" style="outline-style:none;">
                                    <select tabindex="-1">
                                        <option value="Integer">Integer</option>
                                        <option selected="selected" value="Text string">Text string</option>
                                        <option value="Date/time">Date/time</option>
                                        <option value="Floating point">Floating point</option>
                                    </select>
                                </div>
                            </td>
                            <td class="GFNQNVHBD GFNQNVHDE GFNQNVHOD GFNQNVHKD">
                            </tr>
                        </tbody>
                        <tbody style="display: none;">
                            <tfoot style="display: none;" aria-hidden="true"/>
</table>

The error I get is:

    Traceback (most recent call last):
  File "E:\test_runners 2 edit project - add more tests for Regression\selenium_regression_test_5_1_1\Regression_TestCase\RegressionProjectEdit_TestCase.py", line 951, in test_000033_Data_Objects_edit_ACVSEQ2_and_save_edited_changes
    self.assertTrue(data_objects_edit_page.is_value_deleted_from_user_defined_field("Testa"), "User Defiend field Testa has not been deleted")
  File "E:\test_runners 2 edit project - add more tests for Regression\selenium_regression_test_5_1_1\Pages\data_objects_edit.py", line 502, in is_value_deleted_from_user_defined_field
    col_name = row.find_elements(By.TAG_NAME, "td")[1]  # This is the Name column
IndexError: list index out of range

How do I iterate over the table and check "Testa" or for example "AREACODE" is not in the table? "AREACODE" is in the HTML snippet above. I think it is at index column 1

Thanks, Riaz

1 Answer 1

2

IndexError: list index out of range

Actually you're passing hard coded index without checking it's length to determine whether column present or not after deleting the column, that's why it is throwing exception which is absolutely correct because after deleting column from desired row there is no more desire column element present at provided index.

You should try to check column elements length instead to determine whether deleted column element exists or not as below :-

rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows:
      cols = row.find_elements(By.TAG_NAME, "td")         
      if len(cols) > 1 and cols[1].text == name :
            return False 
      return True

It seems to be just getting the header of the table and stopping there. It won't go to the next line which is cols = row.find_elements(By.TAG_NAME, "td")

Actually you need to return True after for loop as below :-

rows = table_id.find_elements(By.TAG_NAME, "tr")
 for row in rows :
      cols = row.find_elements(By.TAG_NAME, "td")         
      if len(cols) > 1 and cols[1].text == name :
            return False 

return True

I could use self.driver.page_source and check in the page source if the text is there or not

If you just want to determine desire text exists on the page or not after delete, you can try as below instead :-

if len(driver.find_elements_by_xpath(".//table[@id='data_configuration_edit_data_object_tab_details_tb_fields']//*[text()= '"+name+"']")) > 0 : 
   return False
return True
Sign up to request clarification or add additional context in comments.

5 Comments

The method is returning true every time even if the name is there. I have stepped through the code and and it won't go to the 2nd iteration in the for loop. At the first iteration for row in rows the value in row is u'Name Type Size'. It seems to be just getting the header of the table and stopping there. It won't go to the next line which is cols = row.find_elements(By.TAG_NAME, "td")
@RiazLadhani Actually you need to return True ooutside the for loop not inside for loop. Thanks
I am still getting the same problem. I could use self.driver.page_source and check in the page source if the text is there or not.
If you just want to determine desire text exists on the page or not, you can try as if len(driver.find_elements_by_xpath(".//*[text()= '"+name+"']")) > 0 : return False
Thanks, I will use this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.