封裝操作表格的公用類(webdriver)

Table.py

1、可以用通過先定位到整個(gè)table表格對(duì)象;
2、再通過表格對(duì)象定位到行對(duì)象;
3、通過行對(duì)象再定位單元格對(duì)象;
4、通過單元格對(duì)象再定位單元格里面的元素對(duì)象

#encoding=utf-8
class Table(object):

    __table = None

    def __init__(self,table):
        self.setTable(table)

    def setTable(self,table):
        self.__table = table#此處傳入的是整個(gè)表格對(duì)象

    def getTable(self):
        return self.__table

    def getRowCount(self):
        #返回table對(duì)象中所有的行tr標(biāo)簽元素對(duì)象
        return len(self.__table.find_elements_by_tag_name("tr"))

    #獲取表格中的列數(shù)
    def getColumnCount(self):
        return len(self.__table.find_elements_by_tag_name("tr")[0].\
         find_elements_by_tag_name("td"))

    #獲取表格中某行某列 單元格對(duì)象
    def getElementCell(self,rowNo,colNo):
        try:
            #先找到某一個(gè)行對(duì)象
            currentRow = self.__table.find_elements_by_tag_name("tr")[rowNo-1]
            #然后根據(jù)行找到該行中的某一列
            currentCol = currentRow.find_elements_by_tag_name("td")[colNo - 1]
            #返回該某行某列的單元格
            return currentCol
        except Exception as e:
            raise e

    #獲取表格中某行某列 單元格中的元素對(duì)象
    def getWebElementInCell(self,rowNo,colNo,by,value):
        #by:定位方式 value:定位表達(dá)式
        try:
            currentRow = self.__table.find_elements_by_tag_name("tr")[rowNo - 1]
            currentCol = currentRow.find_elements_by_tag_name("td")[colNo - 1]

            #然后再根據(jù)單元格找到定位到元素對(duì)象
            element = currentCol.find_element(by,value)

            #返回元素對(duì)象
            return element
        except Exception as e:
            raise e

Test_table.py

#encoding=utf-8
from selenium import webdriver
import time,traceback
from Table import Table
import unittest

class TestTable(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome(executable_path = "d:\\chromedriver")

    def test_table(self):
        url = "http://127.0.0.1/test_table.html"
        self.driver.get(url)
        try:
            #獲取table元素

            table = self.driver.find_element_by_tag_name("table")
            #獲取table實(shí)例對(duì)象
            tableIns = Table(table)

            #獲取表格的行數(shù)
            print(tableIns.getRowCount())

            #獲取表格的列數(shù)
            print(tableIns.getColumnCount())

            #獲取第2行第3列單元格對(duì)象
            cell = tableIns.getElementCell(2,3)
            print(cell)
            print("第2行第3列單元格內(nèi)容: ",cell.text)

            #獲取第2行第3列文本輸入框元素對(duì)象
            cellInput = tableIns.getWebElementInCell(2,3,"tag name","input")
            cellInput.send_keys("第2行第3列輸入框被找到!")
            time.sleep(3)
        except Exception as e:
            print(traceback.print_exc())

    def tearDown(self):
        self.driver.quit()

if __name__== "__main__":
    unittest.main()

網(wǎng)站欄目:封裝操作表格的公用類(webdriver)
本文來源:http://bm7419.com/article6/goscog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、軟件開發(fā)、企業(yè)網(wǎng)站制作、自適應(yīng)網(wǎng)站做網(wǎng)站、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)