代码作用:宽带账号重拨并登录
注意事项:将webdriver放在程序同级目录
方式一:利用系统win32ras
# coding:utf-8
from selenium import webdriver
import time
import win32ras
import os
def Connect(dialname, account, passwd):
dial_params = (dialname, '', '', account, passwd, '')
return win32ras.Dial(None, None, dial_params, None)
def DialBroadband():
dialname = u'宽带连接' # just a name
account = u'123456' #宽带账号
passwd = u'123456' #账号密码
try:
# handle is a pid, for disconnect or showipadrress, if connect success return 0.
# account is the username that your ISP supposed, passwd is the password.
handle, result = Connect(dialname, account, passwd)
if result == 0:
print "Connection success!"
return handle, result
else:
print "Connection failed, wait for 5 seconds and try again..."
time.sleep(5)
DialBroadband()
except:
print "Can't finish this connection, please check out."
return
def Disconnect(handle):
if handle != None:
try:
win32ras.HangUp(handle)
print "Disconnection success!"
return "success"
except:
print "Disconnection failed, wait for 5 seconds and try again..."
time.sleep(5)
Disconnect()
else:
print "Can't find the process!"
return
def Check_for_Broadband():
connections = []
connections = win32ras.EnumConnections()
if (len(connections) == 0):
print "The system is not running any broadband connection."
return
else:
print "The system is running %d broadband connection." % len(connections)
return connections
def ShowIpAddress(handle):
print win32ras.GetConnectStatus(handle)
data = os.popen("ipconfig", "r").readlines()
have_ppp = 0
ip_str = None
for line in data:
if line.find("宽带连接") >= 0:
have_ppp = 1
if have_ppp and line.strip().startswith("IPv4 地址"):
ip_str = line.split(":")[1].strip()
have_ppp = 0
print ip_str
def adsl():
data = Check_for_Broadband()
if data != None:
for p in data:
ShowIpAddress(p[0])
if (Disconnect(p[0]) == "success"):
print "%s has been disconnected." % p[1]
time.sleep(3)
else:
pid, res = DialBroadband()
ShowIpAddress(pid)
time.sleep(3)
return "finsh test"
def login():
fr = open('account.txt') # 打开文件
res = fr.read() # 读取文件的所有内容,类型为string
fr.close()
user_list = res.split() # 默认以空格或者换行符分隔字符串,返回值为list
for i in user_list: # 循环列表,取出每一个值
username, password = i.split(',') # 列表里的每一个值以“,”分隔字符串,“,”前面的是值账号,“,”后面的值是密码
print "user:" + username
print "pawd:" + password
try:
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://accounts.google.com/signin/v2/identifier?passive=true&hl=zh-CN&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fhl%3Dzh-CN%26next%3D%252F%26action_handle_signin%3Dtrue%26app%3Ddesktop&uilel=3&service=youtube&flowName=GlifWebSignIn&flowEntry=ServiceLogin")
time.sleep(3)
elem_user = driver.find_element_by_name("identifier")
elem_user.send_keys(username)
time.sleep(3)
elem_but = driver.find_element_by_id("identifierNext")
elem_but.click()
time.sleep(3)
elem_pass = driver.find_element_by_name("password")
elem_pass.send_keys(password)
time.sleep(2)
elem_but1 = driver.find_element_by_id("passwordNext")
elem_but1.click()
time.sleep(30)
driver.quit()
except:
continue
else:
with open('account.txt', 'r') as r:
lines = r.readlines()
with open('account.txt', 'w') as w:
for l in lines:
if username not in l:
w.write(l)
if __name__ == "__main__":
login()
time.sleep(20)
test = adsl()
print test
方式二:利用cmd命令断开和连接
import os
import time
def connect():
name="宽带连接"
username ="9091186828"
password="123456"
cmd_str=("rasdial %s %s %s" %(name,username,password))
res=os.system(cmd_str)
if res==0:
print ("connect successful")
else:
print (res)
time.sleep(5)
def disconnect():
name="宽带连接"
cmdstr="rasdial %s /disconnect" %name
os.system(cmdstr)
time.sleep(5)
if __name__== "__main__":
disconnect()
connect()