1 from selenium import webdriver 2 from selenium.webdriver import ActionChains 3 4 #1.打开登陆页面 5 wd = webdriver.Chrome() 6 wd.implicitly_wait(10) 7 wd.get('https://mail.qq.com/') 8 9 #2.切换到账号密码登陆 10 login_frame = wd.find_element_by_id('login_frame') 11 wd.switch_to.frame(login_frame) 12 wd.find_element_by_id('switcher_plogin').click() 13 14 #3.输入账号密码 15 u = wd.find_element_by_id('u') 16 u.send_keys('')#---------------此处填QQ号 17 p = wd.find_element_by_id('p') 18 p.send_keys('')#----------------此处填QQ邮箱密码 19 20 #4.点击登陆按钮 21 wd.find_element_by_id('login_button').click() 22 23 #5.点击收信 24 wd.find_element_by_id('folder_1').click() 25 #6.切换到收信iframe框架里 26 main_frame = wd.find_element_by_id('mainFrame') 27 wd.switch_to.frame(main_frame) 28 29 #获取第一个tab页 30 h = wd.current_window_handle 31 32 #通过css选择器,找到我们邮件列表里面的内容 33 email_eles = wd.find_elements_by_css_selector('.toarea .F, .toarea .M') 34 email_eles_count = len(email_eles) 35 #遍历每一封邮件 36 for i in range(email_eles_count): 37 #获取邮件列表的邮件项 38 email_ele = wd.find_elements_by_css_selector('.toarea .F, .toarea .M')[i] 39 #右击邮件项 40 ActionChains(wd).context_click(email_ele).perform() 41 #然后选择在新窗口打开 42 wd.find_elements_by_css_selector('.menu_item')[1].click() 43 #切换到邮件详情页tab 44 email_detail_handle = wd.window_handles[2]#----------此处的数字个人可能不同,1或者2,可自行调试 45 wd.switch_to.window(email_detail_handle) 46 #切换到内容部分 47 main_frame = wd.find_element_by_id('mainFrame') 48 wd.switch_to.frame(main_frame) 49 #获取邮件的标题 50 subject = wd.find_element_by_id('subject').text 51 #获取邮件的内容 52 # content = wd.find_element_by_id('mainContentContainer').text 53 print(subject) 54 #关闭tab页 55 wd.close() 56 #回到第一个tab页,即邮件列表tab 57 wd.switch_to.window(h) 58 #又切换到列表部分 59 main_frame = wd.find_element_by_id('mainFrame') 60 wd.switch_to.frame(main_frame) 61 62 wd.quit()