基于selenium的模拟登陆QQ空间实现
selenium是python的一个第三方库,常用于模拟登陆,本文将实现基于selenium的模拟登陆QQ空间实现。
selenium的安装和环境配置见python自动化处理工具selenium环境搭建
QQ空间登陆界面如下
driver.switch_to.frame("login_frame")
login_frame是frame的name标签
完整代码如下所示:
import random
from selenium import webdriver
import schedule
import time, datetime
def parse_page(username, password):
try:
driver = webdriver.Chrome() #模拟浏览器
url = 'https://i.qq.com/'
driver.get(url)
driver.switch_to.frame("login_frame")
time.sleep(random.randint(5, 10)) #设置时间间隔
#点击输入账号密码
driver.find_element_by_id('switcher_plogin').click()
time.sleep(5)
#输入账号,密码,点击确认
driver.find_element_by_id('u').send_keys(username)
driver.find_element_by_id('p').send_keys(password)
driver.find_element_by_xpath('//input[@type="submit"]').click()
time.sleep(10)
# 获取cookie
# time.sleep(2)
# savedCookies = driver.get_cookies()
# print(savedCookies)
# print(driver.current_url)
driver.find_element_by_xpath('//*[@id="menuContainer"]/div/ul/li[4]/a').click()
time.sleep(10)
print("success")
except Exception as e:
print('打卡出错:', e)
finally:
# 清除缓存,退出
driver.quit()
if __name__ == '__main__':
parse_page('u', 'p')