Python+selenium实现网页批量自动填表格

工作需要填表格,每天一项需保存一次,无法自行批量操作,试了按键精灵之类的…不会用,偶然想起失散多年的py,然后造轮子… 一下午+半个晚上,百度、安装模块、调试模块、百度、组织数据、写代码、调试、百度…过程不讲究,结果OK

import os
import xlrd
import time
from selenium import webdriver

def test():
    _password = "****"
    _user = "****"
    driver = webdriver.Ie("C:/*/IEDriverServer.exe")
    driver.get("http://**.**.**.**/LoginNew.aspx")
    driver.find_element_by_id("ZLTextBox_UserName").send_keys(_user)  # 输入用户名
    driver.find_element_by_id("ZLTextBox_UserPass").send_keys(_password)  # 输入密码
    driver.find_element_by_id("Button_Post").click()  # 点击登陆
    driver.get("http://**.**.**.**/Web/newXmsj/newBggl.aspx")
    driver.find_element_by_id("ctl00_ctl00_main_main_right_ExtGridView_CtrlGridView_ctl02_link").click()  # 选择项目
    #driver.execute_script("javascript:__doPostBack('ctl00$ctl00$main$main_right$SmallGridView$AspNetPager1','13')")
    

    print(len(use_list))
    length = len(use_list)
    for n in range(11,length):
        driver.find_element_by_id("ctl00_ctl00_main_main_right_SmallGridView_btnAdd").click()  # 点击新增
        driver.find_element_by_id("ctl00_ctl00_main_main_right_ChangeNum").send_keys(str(use_list[n]['order']))     #变更编号
        driver.find_element_by_id("ctl00_ctl00_main_main_right_ChangeName").send_keys(str(use_list[n]['name'])) #变更名称
        driver.find_element_by_id("ctl00_ctl00_main_main_right_PfTime").send_keys(str(use_list[n]['pftime']))  #批复日期
        driver.find_element_by_id("ctl00_ctl00_main_main_right_PfMomey").send_keys(str(use_list[n]['pfmoney'])) #批复金额
        driver.find_element_by_id("ctl00_ctl00_main_main_right_ChangeNum").click()
        driver.find_element_by_id("ctl00_ctl00_main_main_right_Button1").click()
        alert = driver.switch_to.alert
        print(alert.text)
        alert.accept()
        driver.find_element_by_id("ctl00_ctl00_main_main_right_Button2").click()


        #driver.get("http://**.**.**.**/Web/newXmsj/newBggl.aspx")
        #driver.find_element_by_css_selectot('input[value='F2']").click()
        # for i in driver.find_element_by_xpath("//*/input[@type='radio']"): # 实现遍历点击所有的radio 
        #     print(i)
        #     sleep(2)
        #     i.click()
        # radios = driver.find_element_by_id("ctl00_ctl00_main_main_right_JYIsHaveList_0")                              #选择有纪要
        # for radio in radios:
        #     radio.click()
        #     time.sleep(2)

# 引用ID对照
    # ctl00_ctl00_main_main_right_ExtGridView_CtrlGridView_ctl02_link 点击标段
    # ctl00_ctl00_main_main_right_TBXXGridView_btnAdd 新增按钮 大于等50万
    # ctl00_ctl00_main_main_right_SmallGridView_btnAdd 新增按钮 小于50万
    # ctl00_ctl00_main_main_right_BeforeMomey 变更前金额
    # ctl00_ctl00_main_main_right_ChangeNum 变更编号
    # ctl00_ctl00_main_main_right_ChangeName 变更名称
    # ctl00_ctl00_main_main_right_PfTime 批复日期
    # ctl00_ctl00_main_main_right_PfMomey 批复金额
    # ctl00_ctl00_main_main_right_PfUnit 批复单位
    # ctl00_ctl00_main_main_right_OrderPfMomey 意向批复金额
    # ctl00_ctl00_main_main_right_OrderPfTime 意向批复日期
    # ctl00_ctl00_main_main_right_OrderPfUnit 意向批复单位
    # ctl00_ctl00_main_main_right_ApplyMomey 承包人申报金额
    # ctl00_ctl00_main_main_right_ApplyTime 承包人申报日期
    # ctl00_ctl00_main_main_right_JYIsHaveList_0 有会议纪要
    # ctl00_ctl00_main_main_right_Button2 返回按钮
    # ctl00_ctl00_main_main_right_Button1 保存按钮

def read_file():
    _file_name = r"bgdata.xls"
    _opsh_name = "TJ4ne"
    _file_data = xlrd.open_workbook(_file_name)# 打开表格
    _opsh_data = _file_data.sheet_by_name(_opsh_name) #打开指定sheet

    _row_num = _opsh_data.nrows # 获取行数
    _row_colnames  = _opsh_data.row_values(0) #第一行索引
    _list = []

    for i in range(1, _row_num):  # 获取第i行的正行的数据
        _row_data = _opsh_data.row_values(i)
        if _row_data:
            _app = {}
            for j in range(len(_row_colnames)): #在这个Excel中,列所在的行有两个数据,所以没循环一行就以这两个数据为键,行数的值为键的值,保存在一个字典里
                _app[_row_colnames[j]] = _row_data[j]
        _list.append(_app)
    return _list

if __name__ == "__main__":

    use_list = read_file()
    print(use_list[0]['beforemoney'])
    test()

    os.system("pause")

点击数:1417

《Python+selenium实现网页批量自动填表格》有2条评论

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

返回顶部