python 写的selenium脚本不能循环,循环时会报 [WinError 10053] 你的主机中的软件中止了一个已建立的连接。错误,求大佬解答下

自学测试的小白 发布于 2018/07/05 13:00
阅读 3K+
收藏 0

用python写了一段自动化脚本,在循环的时候报ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。错误,第一次执行没有问题,就是不能循环,求个大神解答下,代码如下:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
#import shenpiliu
import time
import bs4
import csv
import re
import xlrd
# xpath路径
file = 'C:\\Users\Administrator\Desktop\\2.xlsx'
data = xlrd.open_workbook(file)
table = data.sheets()[0]
i = 1
for p in range(table.nrows-1):
    Driver = webdriver.Chrome()
    import GeneralMethod
    GM = GeneralMethod
    x = 0
    HT_data = table.row_values(i)
    xpathName = '//*[@class="form-control"]'
    xpathPwd = '//*[@type="password"]'
    xpathid = "W2BtnLogin"
    url1 = "http://60.12.232.90:8002/##_TAB"
    url2 = "http://60.12.232.90:8004/workflow/task?TaskId=S0629137fcf5c6b93b6298338f25a8c&ProcessName=%E5%90%88%E5%90%8C%E5%AE%A1%E6%89%B9%E5%8D%95&ProcessVersion=1&FetchDraft=1"
    user = "00019974"
    HTDLeiXuanZe = '//div[@id="ConnectInfo"]//tr[2]/td[1]//span[@class="l-btn-left"]' # 合同大类选择
    HTZLeiXuanZe = '//div[@id="ConnectInfo"]//tr[2]/td[2]//span[@class="l-btn-left"]' # 合同中类选择
    HTXLeiXuanZe = '//div[@id="ConnectInfo"]//tr[2]/td[3]//span[@class="l-btn-left"]' # 合同小类选择
    frame1 = "ifSYS_FORM"
    HTMChengID = "_easyui_textbox_input9"
    HT_Start_dateID = "_easyui_textbox_input36"
    HT__End_dateID = "_easyui_textbox_input37"
    HTKS_Xuanze = '//span[text()="合同开始日期"]/../div/span/span'
    HTKSdate = '//div[@class="panel combo-p"][2]/div//tr//a[text()="今天"]'
    HTJS_Xuanze = '//span[text()="合同结束日期"]/../div/span/span'
    HTJSdate = '/html/body/div[3]/div/div[2]/table/tbody/tr/td[1]/a'
    HT_JinE_ID = "_easyui_textbox_input25"
    WFMC_xpath = '//span[text()="我方名称"]/..//span[text()="选择"]'

    # 数据维护
    HTDL = "收"
    HTZL = "销售合同"
    HTXL = "销售合同"
    HTMC = "加盟合同1"
    HTJE = 100000
    fujian = 'C:\\Users\Administrator\Desktop\\1.docx'

    DengLu = GM.Log_in(HT_data[0],url1,url2)
    time.sleep(2)
    GM.iframe_qiehuan_id('ifSYS_FORM')
    GM.xuanze_click(HTDLeiXuanZe,HT_data[1],1)
    GM.xuanze_click(HTZLeiXuanZe,HT_data[2],2)
    GM.xuanze_click(HTXLeiXuanZe,HT_data[3],2)
    GM.alert() # alert弹出框处理
    GM.ID_text(HTMChengID,HT_data[4])
    GM.Date_XuanZe(HTKS_Xuanze,HTKSdate)
    GM.Date_XuanZe(HTJS_Xuanze,HTJSdate)
    GM.ID_text(HT_JinE_ID,str(HT_data[5]))
    GM.xuanze_click(WFMC_xpath,'',1)
    GM.xuanze_click('//*[@id="CompanyDetailInfo"]/table/tbody/tr[2]/td[1]/div[1]/div/span/a/span/span','',2)
    GM.ID_text('_easyui_textbox_input38',HT_data[6])
    GM.ID_text('_easyui_textbox_input40',HT_data[7])
    GM.ID_text('_easyui_textbox_input41',HT_data[8])
    GM.ID_text('_easyui_textbox_input20',HT_data[9])
    GM.ID_text('_easyui_textbox_input39',str(HT_data[10]))
    GM.ID_text('_easyui_textbox_input22',str(HT_data[11]))
    GM.ID_text('_easyui_textbox_input23',str(HT_data[12]))
    GM.DianJi('//*[@id="nnfegRentPaymentDetail-toolbar"]/a[1]/span/span[1]')
    GM.xuanze_click('//*[@id="divDetails1"]/tbody/tr[2]/td[1]/div[1]/div/span/a/span/span',HT_data[13],1)
    GM.Date_XuanZe('//*[@id="divDetails1"]/tbody/tr[2]/td[2]/div/div/span/span/a','/html/body/div[4]/div/div[2]/table/tbody/tr/td[1]/a')
    GM.Date_XuanZe('//*[@id="divDetails1"]/tbody/tr[2]/td[3]/div/div/span/span/a','/html/body/div[5]/div/div[2]/table/tbody/tr/td[1]/a')
    GM.Date_XuanZe('//*[@id="divDetails1"]/tbody/tr[3]/td[1]/div/div/span/span/a','/html/body/div[6]/div/div[2]/table/tbody/tr/td[1]/a')
    GM.ID_text('_easyui_textbox_input26',str(HT_data[14]))
    GM.ID_text('_easyui_textbox_input27',str(HT_data[15]))
    GM.ID_text('_easyui_textbox_input28',str(HT_data[16]))
    GM.DianJi('//*[@id="nnfegRentPaymentDetail-dialog-buttons"]/a[1]/span/span[1]')
    GM.xpath_text('//*
    GM.SQR_TiJiao("")
    time.sleep(3)
    user1 = GM.RWQD()
    GM.To_quit()
    time.sleep(30)
    continue

加载中
0
自学测试的小白
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoAlertPresentException
import time
import bs4
import csv
import re

Driver = webdriver.Chrome()
def is_element_exist(xpath1): # 判断xpath是否存在
    s1 = Driver.find_elements_by_xpath(xpath1)
    if len(s1) == 0:
        return False
    else:
        return True

def Log_in(username,url,url2):#用户登录
    Driver.get(url)
    Driver.find_element_by_xpath( '//*[@class="form-control"]').send_keys(username)
    Driver.find_element_by_xpath('//*[@type="password"]').send_keys("1")
    Driver.find_element_by_id("W2BtnLogin").click()
    if url2 !="":
        Driver.get(url2)

def iframe_qiehuan_id(frame):
    Driver.switch_to.frame(frame)

def iframe_qiehuan_tga(x):
    frame = Driver.find_elements_by_tag_name('iframe')[x]
    Driver.switch_to.frame(frame)

def xuanze_click(xpath1,text1,x):
    xuanze = Driver.find_element_by_xpath(xpath1)
    xuanze.click()
    iframe_qiehuan_tga(x)
    time.sleep(3)
    if text1 != "":
        if is_element_exist('//span[@class="textbox easyui-fluid"]/input[@id="_easyui_textbox_input1"]'):
            Driver.find_element_by_xpath('//span[@class="textbox easyui-fluid"]/input[@id="_easyui_textbox_input1"]').send_keys(text1)
        elif is_element_exist('//span[@class="textbox easyui-fluid"]//input[@id="_easyui_textbox_input2"]'):
            Driver.find_element_by_xpath('//span[@class="textbox easyui-fluid"]//input[@id="_easyui_textbox_input2"]').send_keys(text1)
        elif is_element_exist('//*[@id="SearchName"]/..//input[@id="_easyui_textbox_input1"]'):
            Driver.find_element_by_xpath('//*[@id="SearchName"]/..//input[@id="_easyui_textbox_input1"]').send_keys(text1)
    if is_element_exist('//*[@id="form1"]/div[3]/div[1]/div/span/a/span/span[1]'):
        Driver.find_element_by_xpath('//*[@id="form1"]/div[3]/div[1]/div/span/a/span/span[1]').click()
        time.sleep(2)
        Driver.find_element_by_xpath('//*[@id="form1"]/div[3]/div[2]/div/div/div/div/div[2]/div[2]/table//tr').click()
    if is_element_exist('//*[@id="form1"]/div[3]/span/a/span'):
        Driver.find_element_by_xpath('//*[@id="form1"]/div[3]/span/a/span').click()
        time.sleep(2)
        Driver.find_element_by_xpath('//*[@id="form1"]/div[4]/div/div[1]/div[2]/div[2]/table/tbody/tr').click()
    if is_element_exist('//*[@id="form1"]/div[3]/span/span/a'):
        Driver.find_element_by_xpath('//*[@id="form1"]/div[3]/span/span/a').click()
        time.sleep(2)
        Driver.find_element_by_xpath('//*[@id="FYLXTree"]/li/div').click()
    if is_element_exist('//div[@class="panel datagrid easyui-fluid"]//div[@class="datagrid-view2"]//tr[@id]'):
        Driver.find_element_by_xpath('//div[@class="panel datagrid easyui-fluid"]//div[@class="datagrid-view2"]//tr[@id]').click()

    Driver.switch_to_default_content()
    iframe_qiehuan_id('ifSYS_FORM')
    Driver.find_element_by_xpath('/html/body/div[13]/div[3]/a[1]/span/span[1]').click()

def ID_text(id,text):
    Driver.find_element_by_id(id).send_keys(text)

def Date_XuanZe(xpath1,xpath2):
    Driver.find_element_by_xpath(xpath1).click()
    Driver.find_element_by_xpath(xpath2).click()

def alert():
    try:
        alert=Driver.switch_to_alert()
        '''添加等待时间'''
        time.sleep(2)
        '''获取警告对话框的内容'''
        print (alert.text)  #打印警告对话框内容
        alert.accept()   #alert对话框属于警告对话框,我们只能接受弹窗
        '''添加等待时间'''
        time.sleep(2)
    except NoAlertPresentException:
        print('没有alert弹出框')

def DianJi(xpath):
    Driver.find_element_by_xpath(xpath).click()

def xpath_text(xpath,text):
    Driver.find_element_by_xpath(xpath).send_keys(text)

def SQR_TiJiao(value):
    Driver.switch_to_default_content()
    Driver.find_element_by_xpath('/html/body/div[3]/div[1]/div[1]/a[1]/span').click()
    Driver.switch_to.frame("ifSYS_FORM")
    if value !="":
        Driver.find_element_by_xpath('//*[@id="nnf-send-confirm"]/table/tbody/tr[2]/td/div/div/span/textarea').send_keys(value)
    else:
        if is_element_exist('//*[@id="nnf-send-confirm"]/table/tbody/tr[2]/td/div/div/span/input[@value=""]'):
            Driver.find_element_by_xpath('//*[@id="nnf-send-confirm"]/table/tbody/tr[2]/td/div/div/span/input[@value=""]').send_keys('摘要未自动生成')
    Driver.find_element_by_xpath('//*[@id="nnf-send-confirm-buttons"]/a[1]').click()

def RWQD():
    time.sleep(3)
    Driver.find_element_by_xpath('//ol[@class="breadcrumb taskviewnavi"]/li[3]/a').click()
    Driver.switch_to.frame('ifSYS_TASKLIST')
    time.sleep(2)
    if is_element_exist("//span[contains(text(),'未办')]/../../../td[2]/span"):
        SPRen = Driver.find_element_by_xpath("//span[contains(text(),'未办')]/../../../td[2]/span").text
        if len(SPRen)<9:
            username1 = 'sa'
        else:
            username1 = str(SPRen)[-9:-1]
        return username1
    else:
        return 0
    Driver.switch_to_default_content()


def To_quit():
    Driver.quit()

def SPR_TiJiao(username,url):
    Driver = webdriver.Chrome()
    Driver.get(url)
    Driver.find_element_by_xpath( '//*[@class="form-control"]').send_keys(username)
    Driver.find_element_by_xpath('//*[@type="password"]').send_keys("1")
    Driver.find_element_by_id("W2BtnLogin").click()
    time.sleep(5)
    Driver.switch_to.frame("if")  # 定位到首页iframe
    time.sleep(2)
    DJLCheng = Driver.find_element_by_xpath('//tr[@class="read ng-scope"][1]/td[3]//span') # 点击流程
    DJLCheng.click()
    time.sleep(3)
    Driver.switch_to_window(Driver.window_handles[1])  # 获取新页面
    time.sleep(3)
    Driver.find_element_by_xpath('//div[@class="mail-body text-right tooltip-demo ng-scope"]/a').click()  # 提交
    dialog_box = Driver.switch_to_alert()
    time.sleep(1)
    dialog_box.accept()  # 点击确定
    time.sleep(5)
    #Driver.switch_to_default_content()

def read_file(path):
    data = xlrd.open_workbook(path)
    table = data.sheets()[0]
oschina二把刀
oschina二把刀
回复 @自学测试的小白 : 我也遇到了同样的问题,请问你是怎么解决的。
自学测试的小白
这是调用的方法,所有操作和定位都在这里进行,主程序只做数据传递
返回顶部
顶部