[Selenium] 處理表格(python + java)

python :javascript

https://www.cnblogs.com/yan-xiang/p/6819168.htmlhtml

操做內容:獲取table總行數、總列數、獲取某單元格的text值,刪除一行【若是每行後邊提供刪除的按鈕】java

 

案例:python

HTML代碼:web

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<script type="text/javascript"> 
function deleteRow(tableID, obj) {//參數爲表格ID,觸發對象 
//得到觸發對象的行號,parentElement的個數取決於觸發對象爲TR的第幾級子項,input=>td=>tr,因此parentElement有兩個 
var rowIndex = obj.parentElement.parentElement.rowIndex; 
//var table = document.getElementById(tableID).deleteRow(rowIndex); 
alert("您肯定要刪除嗎?") 
obj.parentElement.parentElement.parentElement.deleteRow(rowIndex); //再簡化:省略tableID參數 

</script> 
<table border="1" id="tab1" width="90%">
<tbody>
<tr>
<th>序號</th>
<th>姓名</th>
<th>年齡</th>
<th>身份證</th>
<th>刪除</th>
</tr>
<tr>
<td style="text-align: center;">1</td>
<td >測試一</td>
<td>15</td>
<td>1111111111111111111</td>
<td name="sc" style="text-align: center;"><input type="button" value="刪除" onclick="deleteRow('tb',this)"/></td>
</tr>
<tr>
<td style="text-align: center;">2</td>
<td>測試二</td>
<td>20</td>
<td>2222222222222222222</td>
<td name="sc" style="text-align: center;"><input type="button" value="刪除" onclick="deleteRow('tb',this)"/></td>
</tr>測試

<tr>
<td style="text-align: center;">3</td>
<td>測試三</td>
<td>20</td>
<td>3333333333333333333</td>
<td name="sc" style="text-align: center;"><input type="button" value="刪除" onclick="deleteRow('tb',this)"/></td>
</tr>ui

<tr>
<td style="text-align: center;">4</td>
<td>測試四</td>
<td>20</td>
<td>4444444444444444444</td>
<td name="sc" style="text-align: center;"><input type="button" value="刪除" onclick="deleteRow('tb',this)"/></td>
</tr>this


</tbody>
</table>spa

</body>
</html>code

Python代碼:

# -*- 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的總行數,包含標題
table_rows = table.find_elements_by_tag_name('tr')
print "總行數:",len(table_rows)

#tabler的總列數
'''
在table中找到第一個tr,以後在其下找到全部的th,便是tabler的總列數
'''
table_cols = table_rows[0].find_elements_by_tag_name('th')
print "總列數:",len(table_cols)

#獲取某單元格的text:獲取第一行第二列的text,[不算標題行]
row1_col2 = table_rows[1].find_elements_by_tag_name('td')[1].text
print "第一行第二列的text:",row1_col2

#刪除最後一行
table_rows[-1].find_element_by_tag_name('input').click()
time.sleep(2)
driver.switch_to_alert().accept()
time.sleep(2)

#檢查是否刪除成功
'''
解釋:text_to_be_present_in_element實際是一個類,因爲添加了__call__方法,它的實例也能夠調用
做用:檢查指定元素上的文本是否符合預期

檢查是否成功的思路
1.刪除的是最後一行,也就是序號爲'4'的那行,因此只要檢查整個table中是否還有‘4’便可,若是返回false,說明刪除成功,前提條件是table中的其餘單元格內容均不包含‘4’
【若是其餘單元格中的內容包含‘4’,返回True,顯然檢查結果是不正確的,因此此處要檢查的預期值必須在table中是惟一的,好比用身份證】
2.檢查table中最後一行的序號是否是‘4’,若是不是,則說明刪除成功
3.經過檢查table的行數來判斷
'''
#第一種檢查
expected_4 = EC.text_to_be_present_in_element((By.XPATH,'//*[@id="tab1"]'),'4')
if expected_4(driver) == False:
print '第一種檢查:刪除成功'
#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 '第二種檢查:刪除成功'
#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 '第三種檢查:刪除成功'

運行結果:

 

 

 

JAVA :

import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.WebElement;  
import org.testng.annotations.Test;  
public class LocateTable {  
  public WebDriver driver;  
  @Test  
  public void LocateTableTest() {  
    WebElement table = driver.findElement(By.id("table"));  
    List<WebElement> rows = table.findElements(By.tagName("tr"));  
    // assertEquals(5,rows.size());  
    for(WebElement row:rows){  
       List<WebElement> cols= row.findElements(By.tagName("td"));  
       for(WebElement col:cols){  
          System.out.println(col.getText()+"\t");               
        }  
     System.out.println("");  
    }  
  }  
} 
相關文章
相關標籤/搜索