用selenium做自动化非常方便,以下代码就是通过selenium实现自动登录csdn,代码如下:
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
userName="{your user name}"
password="{your password}"
webdriver = webdriver.Chrome()
webdriver.get('https://csdn.net')
time.sleep(5)
##登录按扭
btnLogin = webdriver.find_element(By.CLASS_NAME, "toolbar-btn-loginfun")
btnLogin.click()
#停留3秒
time.sleep(3)
#找到弹出窗iframe,并切换到iframe
loginFrame = webdriver.find_element(By.XPATH, '//iframe[@name="passport_iframe"]')
webdriver.switch_to.frame(loginFrame)
#切换到密码登录
mmSpan = webdriver.find_element(By.XPATH, '/html/body/div/div/div/div[2]/div[2]/div/div[1]/span[4]')
mmSpan.click()
time.sleep(2)
#用户名
InputUserName = webdriver.find_element(By.XPATH,"/html/body/div/div/div/div[2]/div[2]/div/div[2]/div/div[1]/div/input")
InputUserName.send_keys(userName)
#密码
InputPassword = webdriver.find_element(By.XPATH,"/html/body/div/div/div/div[2]/div[2]/div/div[2]/div/div[2]/div/input")
InputPassword.send_keys(password)
#勾选协议
chekBox = webdriver.find_element(By.XPATH,'/html/body/div/div/div/div[2]/div[2]/div/div[2]/p/div/i')
chekBox.click()
#登录提交按扭
submitBox = webdriver.find_element(By.XPATH,'/html/body/div/div/div/div[2]/div[2]/div/div[2]/div/div[4]/button')
submitBox.click()
上述本人测试通过,大家可以放心使用