Selenium_python test object operation: Table table

Source: Internet
Author: User

What to do: Gets the total number of rows in the table, the number of columns, gets the text value of a cell, and deletes a row "If a Delete button is provided behind each line"

Case:

HTML code :

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<body>
<script type= "Text/javascript" >
function DeleteRow (TableID, obj) {//parameter is table ID, trigger object
To get the line number of the trigger object, the number of parentelement depends on the first few sub-items of the Trigger object TR, INPUT=&GT;TD=&GT;TR, so Parentelement has two
var rowIndex = Obj.parentElement.parentElement.rowIndex;
var table = document.getElementById (TableID). DeleteRow (RowIndex);
Alert ("Are you sure you want to delete it?") ")
Obj.parentElement.parentElement.parentElement.deleteRow (RowIndex); Re-simplification: omitting TableID parameters
}
</script>
<table border= "1" id= "Tab1" width= "90%" >
<tbody>
<tr>
<th> Serial Number </th>
<th> name </th>
<th> Age </th>
<th> ID Card </th>
<th> Delete </th>
</tr>
<tr>
&LT;TD style= "Text-align:center;" >1</td>
&LT;TD > Test One </td>
<td>15</td>
<td>1111111111111111111</td>
&LT;TD name= "SC" style= "text-align:center;" ><input type= "button" value= "delete" onclick= "deleterow (' TB ', this)"/></td>
</tr>
<tr>
&LT;TD style= "Text-align:center;" >2</td>
<td> Test Two </td>
<td>20</td>
<td>2222222222222222222</td>
&LT;TD name= "SC" style= "text-align:center;" ><input type= "button" value= "delete" onclick= "deleterow (' TB ', this)"/></td>
</tr>

<tr>
<TD style= "Text-align:center;" >3</td>
<td> Test Three </td>
<td>20</td>
<td>3333333333333333333</td>
<TD name= "SC" style= "text-align:center;" ><input type= "button" value= "delete" onclick= "deleterow (' TB ', this)"/></td>
</tr>

<tr>
<TD style= "Text-align:center;" >4</td>
<td> Test Four </td>
<td>20</td>
<td>4444444444444444444</td>
<TD name= "SC" style= "text-align:center;" ><input type= "button" value= "delete" onclick= "deleterow (' TB ', this)"/></td>
</tr>


</tbody>
</table>

</body>

Python code:

#-*-Coding:utf-8-*-
From selenium import Webdriver
Import Os,time
From selenium.webdriver.common.by Import by
From Selenium.webdriver.support import expected_conditions as EC

File_path = Os.path.abspath (' selenium_table.htm ')
Driver = Webdriver. Chrome ()
Driver.get (File_path)
Table = driver.find_element_by_id (' tab1 ')

#table的总行数, including the title
Table_rows = Table.find_elements_by_tag_name (' tr ')
Print "Total number of rows:", Len (table_rows)

#tabler的总列数
‘‘‘
Find the first TR in table, then find all the th under it, that is, the total number of columns of Tabler
‘‘‘
Table_cols = table_rows[0].find_elements_by_tag_name (' th ')
Print "Total number of columns:", Len (Table_cols)

#获取某单元格的text: Gets the text,[of the second column of the first row does not count as the header row]
Row1_col2 = Table_rows[1].find_elements_by_tag_name (' TD ') [1].text
Print "text in the second column of the first row:", row1_col2

#删除最后一行
Table_rows[-1].find_element_by_tag_name (' input '). Click ()
Time.sleep (2)
Driver.switch_to_alert (). Accept ()
Time.sleep (2)

#检查是否删除成功
‘‘‘
Explanation: Text_to_be_present_in_element is actually a class, and because of the addition of the __call__ method, its instances can also be called
Function: Checks whether the text on the specified element matches the expected

Check whether the idea of success
1. Delete the last line, that is, the serial number is ' 4 ', so as long as the entire table to check if there is still ' 4 ', if the return false, the deletion succeeds, if the other cell contents in the table does not contain ' 4 '
"If the contents of other cells contain ' 4 ' and return true, it is obvious that the check result is incorrect, so the expected value to be checked here must be unique in the table, such as with an ID"
2. Check that the ordinal of the last row in the table is ' 4 ', if not, the deletion succeeds
3. Check the number of rows in the table to determine
" "
#第一种检查
Expected_4 = Ec.text_to_be_present_in_element ((By.xpath, '//*[@id = "tab1"] '), ' 4 ')
If Expected_4 (driver) = = False:
print ' first check: Delete succeeded '
#time. Sleep (2)
#第二种检查
Expected_4 = Ec.text_to_be_present_in_element ((By.xpath, '//*[@id = "tab1"]/tbody/tr[4]/td[1] '), ' 4 ')
If Expected_4 (driver)! = True:
print ' second check: Delete succeeded '
#time. Sleep (2)
#第三种检查
TABLE_ROWS_SC = Table.find_elements_by_tag_name (' tr ')
Print Len (TABLE_ROWS_SC)
If Len (table_rows_sc) = = Len (table_rows)-1:
print ' third check: Delete Successful '

Operation Result:

Selenium_python test object operation: Table table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.