第五篇 - Selenium突破反爬获取qq邮件标题

时间:2021-05-08 03:36:21
 from selenium import webdriver
from selenium.webdriver import ActionChains #1.打开登陆页面
wd = webdriver.Chrome()
wd.implicitly_wait(10)
wd.get('https://mail.qq.com/') #2.切换到账号密码登陆
login_frame = wd.find_element_by_id('login_frame')
wd.switch_to.frame(login_frame)
wd.find_element_by_id('switcher_plogin').click() #3.输入账号密码
u = wd.find_element_by_id('u')
u.send_keys('')#---------------此处填QQ号
p = wd.find_element_by_id('p')
p.send_keys('')#----------------此处填QQ邮箱密码 #4.点击登陆按钮
wd.find_element_by_id('login_button').click() #5.点击收信
wd.find_element_by_id('folder_1').click()
#6.切换到收信iframe框架里
main_frame = wd.find_element_by_id('mainFrame')
wd.switch_to.frame(main_frame) #获取第一个tab页
h = wd.current_window_handle #通过css选择器,找到我们邮件列表里面的内容
email_eles = wd.find_elements_by_css_selector('.toarea .F, .toarea .M')
email_eles_count = len(email_eles)
#遍历每一封邮件
for i in range(email_eles_count):
#获取邮件列表的邮件项
email_ele = wd.find_elements_by_css_selector('.toarea .F, .toarea .M')[i]
#右击邮件项
ActionChains(wd).context_click(email_ele).perform()
#然后选择在新窗口打开
wd.find_elements_by_css_selector('.menu_item')[1].click()
#切换到邮件详情页tab
email_detail_handle = wd.window_handles[2]#----------此处的数字个人可能不同,1或者2,可自行调试
wd.switch_to.window(email_detail_handle)
#切换到内容部分
main_frame = wd.find_element_by_id('mainFrame')
wd.switch_to.frame(main_frame)
#获取邮件的标题
subject = wd.find_element_by_id('subject').text
#获取邮件的内容
# content = wd.find_element_by_id('mainContentContainer').text
print(subject)
#关闭tab页
wd.close()
#回到第一个tab页,即邮件列表tab
wd.switch_to.window(h)
#又切换到列表部分
main_frame = wd.find_element_by_id('mainFrame')
wd.switch_to.frame(main_frame) wd.quit()