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

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time, traceback

driver = None
wait = 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()

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

时间: 2024-08-29 12:14:59

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

python webdriver firefox 登录126邮箱,先添加联系人,然后进入首页发送邮件,带附件。

代码:#encoding=utf-8from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_condition

python2+selenium+mail,自动登录126邮箱

在进行登录126邮箱时有几个坑,要完美避过可以看一下下文,直接上代码: #encoding = utf-8 from selenium import webdriverimport unittestimport timeclass login126(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(executable_path = "chromedriver") def test_login(sel

c# winform 程序用126邮箱发邮件

代码如下: private void button1_Click(object sender, EventArgs e) { string strTitle = "alert! We have not receive new data from mera over 20 mins"; try { SmtpClient client = new SmtpClient("smtp.126.com", 25) { Credentials = new NetworkCred

Selenium登录126邮箱,定位不到账号输入框解决办法

用firebug去定位126邮箱登录页面的账号密码输入框,查看元素时发现ID每次都是变动的,不能用ID定位 使用xpath定位,发现每次定位都会失败,后来发现这个登录的账号密码输入框,是嵌套在表单iframe中,所以定位时,要先进入iframe里面才能正确定位 iframe/frame的定位方式有3种,一是通过ID,而是通过name,三是xpath 例如: switch_to_frame(id): switch_to_frame(name): 若是没有ID和name,那么只能通过xpath方式定

Python3实现126邮箱163邮箱SMTP发送邮件

Email起到提醒作用,当爬虫过程中遇到异常或者服务器遇到问题,可以通过Email及时向自己报告.发送邮件的协议是STMP,python内置对SMTP的支持,可以发送纯文本邮件.html邮件以及带附件的邮件. SMTP协议 首先了解SMTP(简单邮件传输协议),邮件传送代理程序使用SMTP协议来发送电邮到接收者的邮件服务器.SMTP协议只能用来发送邮件,不能用来接收邮件,而大多数的邮件发送服务器都是使用SMTP协议.SMTP协议的默认TCP端口号是25. 发送邮件之前的配置: 上面说了是使用SM

【selenium+Python unittest】之发送邮箱时报错:smtplib.SMTPDataError、smtplib.SMTPAuthenticationError(例:126邮箱)

原代码如下: import smtplib from email.mime.text import MIMEText from email.header import Header #要发送的服务器 smtpserver = 'smtp.126.com' #要发送的邮箱用户名/密码 user = 'XXX@126.com' password = 'XXX' #发送的邮箱 sender = 'XXX@126.com' #接收的邮箱 receiver = 'XXX@qq.com' #发送邮箱主题 s

(java)selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出

selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出: 该情况适合能能循环page=1~n,并且每个网页随着循环可以打开的情况, 注意一定是自己拼接的url可以打开,如:http://ask.testfan.cn/articles?page=15,就可以翻到文章分类的第15页: import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.u

python webdriver 显示等待-自动登录126邮箱,添加联系人

脚本内容:#encoding=utf-8#author-夏晓旭from selenium import webdriverimport timefrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.common.exceptions import TimeoutException, NoSuchElementExceptioni

ofbiz 本地化及邮件设置126邮箱

ofibz登陆功能有通过电子邮件找会密码的功能,但找回密码功能需要配置一个发送email的邮箱账号和smtp服务器的配置,具体配置如下: 1:在ofbiz数据库的表product_store_email_setting中找到from_address字段,将该字段中值全部修改成配置的默认发送账号 2:在general.properties中配置smtp参数 # -- The default domainname used in the notification emails links # as