## 用户名校验
d = {'username':'jone','password':'123456'}
name = input('please enter your name:')
while name != d['username']:
if name == '':
name = input('please enter your name:')
else:
name = input('user name does not exist,please reenter your name:')
## 密码校验
word = input('please enter your password:')
count = 3
while word != d['password']:
count -= 1
if count > 0:
word = input('you have %s chance to enter psssword' %count)
else:
print('more than 3 times error, and the account has been frozen')
break
if word == d['password']:
print('Login success')
2. 百度登录
## 统一编码格式
# coding:utf-8
## 导入webdriver已备引用api
from selenium import webdriver
import time
def findId(id):
e = driver.find_element_by_id(id)
return e
def findLinkText(text):
e = driver.find_element_by_link_text(text)
return e
## 获取浏览器对象
driver = webdriver.Chrome()
## 浏览器全屏
driver.maximize_window()
## 隐式等待30s
driver.implicitly_wait(30)
driver.get('https://www.baidu.com')
## 搜索框输入内容
findId("kw").send_keys('selenium')
## 点击操作
findId("su").click()
## login
findLinkText('登录').click()
findId('TANGRAM__PSP_10__footerULoginBtn').click()
findId('TANGRAM__PSP_10__userName').send_keys('zyanwei2011')
time.sleep(5)
findId('TANGRAM__PSP_10__password').send_keys('zy383417')
## 此处等待是为了避开百度的检查机制,太快会进行手机验证
time.sleep(5)
findId('TANGRAM__PSP_10__submit').click()
element1 = findId('user')
print(element1.text)
## 下部分为获取浏览器token,然后使用token的登陆
# 获取token
cookies = driver.get_cookies()
# 清楚浏览器token,使处于未登陆状态
driver.delete_all_cookies()
driver.refresh()
time.sleep(2)
# 添加cookies
for i in cookies:
driver.add_cookie({
'domain': '.baidu.com', # 此处xxx.com前,需要带点
'name': i['name'],
'value': i['value'],
'path': '/',
'expires': None
})
# 刷新页面,检查是否登陆
driver.refresh()
1. 页面登录## 用户名校验d = {'username':'jone','password':'123456'}name = input('please enter your name:')while name != d['username']: if name == '': name = input('please enter your name:') ...