Python+Selenium+Mysql(動態擷取資料,資料庫互動)

來源:互聯網
上載者:User

標籤:cti   har   common   turn   partial   class   user   元組   語句   

一、建立資料庫連接
#!coding:utf-8import pymysql‘‘‘Python3之後不再支援MySQLdb的方式進行訪問mysql資料庫;可以採用pymysql的方式串連方式:    1、導包        import pymysql    2、開啟資料庫連接        conn = pymysql.connect(host=‘10.*.*.*‘,user=‘root‘,password=‘123456‘,db=‘self_dev‘,charset=‘utf8‘,cursorclass=pymysql.cursors.DictCursor)        備忘:其中cursorclass=pymysql.cursors.DictCursor 可有可無,配置的是每個欄位的展示方式,按照字典的形式進行展示(方便通過列名進行訪問),預設元組形式。    3、使用cursor()方法擷取操作遊標         cur = connection.cursor()    4、SQL 查詢語句        sql = "SELECT * FROM table t where t.name=‘政協‘"    5、執行SQL語句        cur.execute(sql)    6、擷取所有記錄列表        rows = cur.fetchall()    7、輸出    8、關閉資料庫連接        connection.close()‘‘‘class dbClection:    connection = pymysql.connect(host=‘10.*.*.*‘,        user=‘root‘,        password=‘123456‘,        db=‘self_dev‘,        charset=‘utf8‘,        cursorclass=pymysql.cursors.DictCursor)    def getOrgid(self):        cur = self.connection.cursor()        sql = "SELECT t.id FROM org t where t.name=‘自動化添加(行政地區)‘ and t.status=‘1‘"        try:            cur.execute(sql)            results = cur.fetchall()            for row in results:                org_id = row[‘id‘]                return org_id                #print(org_id)        except:            print(‘Error:unable to fetch data‘)        self.connection.close()‘‘‘if __name__ == ‘__main__‘:    db = dbClection()    res = db.getOrgid()    print(res)‘‘‘
二、引用上述建立的dbClection下的getOrgid()方法
#!coding:utf-8from selenium import webdriverfrom time import sleepimport unittestfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.support.ui import Selectfrom mySql import dbClection


----------主體省略----------------

def test_add_fenwei(self):        u‘‘‘登入‘‘‘        username = ‘org-admin‘        password = ‘123456‘        self.login(username,password)         sleep(2)        #進入組織管理介面        self.dr.get(self.org_url)        sleep(2)        db = dbClection()        xzjd_org = db.getOrgid()        # print(xzjd_org)        self.by_xpath(‘//*[@id="%s"]/i‘%xzjd_org).click()        sleep(2)        self.dr.find_element_by_partial_link_text(‘自動化添加(地區)‘).click()        sleep(2)        self.by_id(‘addDept‘).click()        sleep(3)        self.by_id(‘orgName_add‘).send_keys(‘自動化添加(分類)‘)        sleep(2)        Select(self.by_id("govDeptType_add")).select_by_visible_text("分類")        sleep(2)        self.by_id(‘saveDeptAdd‘).click()


  備忘:變數在使用在頁面定位中時,需要進行%格式化,否則無法傳入。
      例如:
 
db = dbClection()
 xzjd_org = db.getOrgid() # print(xzjd_org)        self.by_xpath(‘//*[@id="%s"]/i‘%xzjd_org).click()



Python+Selenium+Mysql(動態擷取資料,資料庫互動)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.