selenium 与 chrome 进行qq登录并发邮件操作实例详解
出现的问题:
qq邮箱各种iframe需要切换,延时是必须的,通过各种方法找元素,qq邮件正文的iframe name是变化的,其他几种方法都不行,最后居然用这样搞定。o[0].click() , o[0].send_keys("abc"),还得再研究研究!!!
备注:已经在机器上登录过QQ客户端,XXXX是发送QQ号,YYYYY是接受QQ号
from selenium import webdriver import time import sys if 1: driver=webdriver.Chrome() driver.get('http://www.qq.com') qq_handle = driver.current_window_handle driver.find_element_by_xpath('''//*[@id="loginGrayLayout"]''').click() time.sleep(1) driver.switch_to.frame('login_frame') driver.find_element_by_xpath('''//*[@id="img_out_XXXXX"]''').click() time.sleep(1) driver.refresh() driver.find_element_by_xpath('''//*[@id="mailGrayIconLogin"]/a''').click() all_handles = driver.window_handles time.sleep(1) driver.switch_to.window(all_handles[1]) driver.find_elements_by_css_selector('#composebtn')[0].click() # time.sleep(1) driver.switch_to.frame('mainFrame') driver.find_element_by_xpath('''//*[@id="toAreaCtrl"]/div[2]/input''').send_keys("XXXXX@vip.qq.com") driver.find_element_by_xpath('''//*[@id="subject"]''').send_keys("Test Mail Subject") o=driver.find_elements_by_class_name("qmEditorIfrmEditArea") o[0].click() o[0].send_keys("Hello world!") driver.find_element_by_xpath('''//*[@id="toolbar"]/div/a[1]''').click() sys.exit(0) driver=webdriver.Chrome() driver.get('http://mail.qq.com') driver.switch_to.frame("login_frame") driver.find_element_by_xpath('''//*[@id="img_out_XXXXX"]''').click() time.sleep(1) driver.find_elements_by_css_selector('#composebtn')[0].click() time.sleep(1) driver.switch_to.frame('mainFrame') time.sleep(1) driver.find_element_by_xpath('''//*[@id="toAreaCtrl"]/div[2]/input''').send_keys("YYYYY@vip.qq.com") driver.find_element_by_xpath('''//*[@id="subject"]''').send_keys("Test Mail Subject") o=driver.find_elements_by_class_name("qmEditorIfrmEditArea") o[0].click() #must click.... shit!!!! o[0].send_keys("abc") driver.find_element_by_xpath('''//*[@id="toolbar"]/div/a[1]''').click()