代码如下:
from selenium import webdriver import time browser = webdriver.Chrome(r'C:\Users\Administrator\Desktop\chromedriver.exe') headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', } browser.get('http://localhost:800/admin/list.aspx') def button(w): xx = browser.find_elements_by_css_selector('button') for i in xx: if w in i.text: print(w) i.click() break else: pass def text(): txt = open(r'C:\Users\Administrator\Desktop\syfw\shiyefuwu\test.txt') txt = txt.read() txt = txt.split('\n') #print(txt) return txt def tian(name): button('添加站点') browser.switch_to.frame(0) x=browser.find_element_by_id('mc') x.send_keys(name) y=browser.find_element_by_id('url') y.send_keys(name) z=browser.find_element_by_xpath('/html/body/div/div/form/div[5]/div/div[1]/i') z.click() browser.find_element_by_id('Button1').click() time.sleep(1) alert = browser.switch_to.alert #切到弹出框 print(alert.text) a=alert.text alert.accept() #确定 return '添加成功!' in a def mian(): txt = text() while len(txt)!=0: time.sleep(1) name = txt[0] try: s = tian(name) if s: print(name+'成功!') txt.pop(0) else: pass except: print(name,end='') print('————出错5秒后重试') time.sleep(2) browser.refresh() time.sleep(3) mian()