分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

webdriver实现用126邮箱给你自己发一个带有附件、标题、正文的邮件

发布时间:2023-09-06 02:32责任编辑:沈小雨关键词:邮件
from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byfrom selenium.common.exceptions import TimeoutException, NoSuchElementExceptionimport time, tracebackdriver = Nonewait = None#登录函数def login_126(): ???global driver ???global wait ???url = "http://www.126.com" ???driver = webdriver.Chrome(executable_path="d:\\chromedriver") ???driver.get(url) ???# 定义显式等待对象 ???wait = WebDriverWait(driver, 10, 0.2) ???try: ???????# 切入登录框所在的iframe ???????wait.until(EC.frame_to_be_available_and_switch_to_it(( ????????????By.XPATH, ‘//iframe[contains(@id,"x-URS-iframe")]‘))) ???????time.sleep(3) ???????# driver.switch_to.frame(driver.find_element_by_xpath( ???????# ‘//iframe[contains(@id,"x-URS-iframe")]‘)) ???????# 显式等待用户名输入框可见且获取输入框元素对象 ???????username = wait.until(lambda x: x.find_element_by_xpath( ????????????‘//input[@placeholder="邮箱帐号或手机号"]‘)) ???????username.send_keys("xxxx") ???????# 直接获取密码输入框 ???????passwd = driver.find_element_by_xpath(‘//input[@name="password"]‘) ???????passwd.send_keys("xxxxx") ???????# 获取登录按钮并点击 ???????driver.find_element(By.ID, "dologin").click() ???????#且出到正文 ???????driver.switch_to.default_content() ???????# 显式等待登录成功后“退出”按钮出现在页面上 ???????if wait.until(EC.visibility_of_element_located((‘xpath‘, ‘//a[.="退出"]‘))): ???????????print("登录成功!") ???????else: ???????????print("登录失败!") ???except NoSuchElementException as e: ???????print(traceback.print_exc()) ???except TimeoutException as e: ???????print(traceback.print_exc()) ???except Exception as e: ???????print(traceback.print_exc())#退出驱动并关闭所有浏览器窗口def close_driver(): ???global driver ???driver.quit()def send_mail(): ???global driver ???global wait ???try: ???????#显式等待写信按钮可点击并获取写信按钮元素对象 ???????write_button = wait.until(EC.element_to_be_clickable((‘xpath‘,‘//span[.="写 信"]‘))) ???????write_button.click() ???????#显式等待收件人输入框可见 ???????receiver = wait.until(lambda x : x.find_element_by_xpath( ????????????????‘//input[@class="nui-editableAddr-ipt"]‘)) ???????#receiver = wait.until(EC.visibility_of_element_located((‘xpath‘,‘//span[.="写 信"]‘))) ???????receiver.send_keys("hhq8286@126.com") ???????#获取主题输入框页面元素对象 ???????subject = driver.find_element_by_xpath(‘//input[contains(@id,"subjectInput")]‘) ???????subject.send_keys("2019你要怎么做?") ???????#获取添加附件元素对象(用send_keys()需要定位到input元素) ???????attachment = driver.find_element_by_xpath(‘//input[@class="O0"]‘) ???????attachment.send_keys("e:\\python\\111.txt") ???????#等待附件上传完成 ???????wait.until(EC.visibility_of_element_located((‘xpath‘,‘//span[.="上传完成"]‘))) ???????print("附件上传完成!") ???????time.sleep(3) ???????#切入正文frame ????????wait.until(EC.frame_to_be_available_and_switch_to_it( ????????????(‘xpath‘,‘//iframe[@class="APP-editor-iframe"]‘))) ???????contentBox = driver.find_element_by_xpath(‘/html/body‘) ???????contentBox.send_keys("学习 理财 看书 如何转更多钱钱,不为钱而工作") ???????#切出frame,切到正文 ???????driver.switch_to.default_content() ???????#发送 ???????driver.find_element_by_xpath( ???????????‘//footer[@class="jp0"]//span[text()="发送"]//preceding-sibling::span//b‘).click() ???????#显式等待发送成功的//h1[contains(@id,"succInfo")] ???//h1[text()="发送成功"] ???????if wait.until(EC.visibility_of_element_located((‘xpath‘,‘//h1[text()="发送成功"]‘))): ???????????print("邮件发送成功!") ???????else: ???????????print("邮件发送失败!") ???????time.sleep(5) ???except NoSuchElementException as e: ???????print(traceback.print_exc()) ???except TimeoutException as e: ???????print(traceback.print_exc()) ???except Exception as e: ???????print(traceback.print_exc())def main(): ???login_126() ?????send_mail() ???close_driver()if __name__ == "__main__": ???main()

webdriver实现用126邮箱给你自己发一个带有附件、标题、正文的邮件

原文地址:http://blog.51cto.com/13496943/2349807

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved