import requests,bs4,urllib,os
from selenium import webdriver
from time import sleep

#获取当天本地天气预报
#URL:http://www.weather.com.cn/weather/101180101.shtml
resWeather = urllib.request.urlopen('http://www.weather.com.cn/weather/101180101.shtml')
resWeatherHtml = str(resWeather.read(),'utf-8')


#获取天气信息
bs4Weather = bs4.BeautifulSoup(resWeatherHtml)
soupCity = bs4Weather.select('input[id="hidden_title"]')


#拼接邮件内容
maileNeiRong = "亲爱的,今天郑州的天气是:     "+'\n'+soupCity[0].get('value')+"!"+'\n'+" 今天有个好心情!"+'\n''\n''\n'+'         爱你的人!'
print(maileNeiRong)

#由于多次访问163邮箱需要经过验证码验证,现在由hao123转到163邮箱
browserHao123_163Email = webdriver.Firefox()
browserHao123_163Email.get('https://www.hao123.com/')

#获取hao123主页的句柄
handle_hao123 = browserHao123_163Email.current_window_handle

browserHao123_163Email.find_element_by_id('mail').click() 
browserHao123_163Email.find_element_by_id('mailName').clear()
browserHao123_163Email.find_element_by_id('mailName').send_keys('邮箱用户名')
browserHao123_163Email.find_element_by_id('mailPwd').clear()
browserHao123_163Email.find_element_by_id('mailPwd').send_keys('邮箱密码')
browserHao123_163Email.find_element_by_id('mailSubmit').click() 

sleep(5)
#在表单里面操作完毕后,无论页面是否进行跳转,必须有退出表单的操作
browserHao123_163Email.switch_to_default_content() 

"""
#自动登录163邮箱,不经过hao123
#http://mail.163.com/
browser163Email = webdriver.Firefox()
browser163Email.get('http://mail.163.com/')

browser163Email.switch_to_frame('x-URS-iframe')#切换frame(iframe)


#输入用户名和密码登录邮箱
browser163Email.find_element_by_name('email').clear()
browser163Email.find_element_by_name('email').send_keys('peng_zhi198976')
browser163Email.find_element_by_name('password').clear()
browser163Email.find_element_by_name('password').send_keys('pengzhi198976')
browser163Email.find_element_by_id('dologin').click()  
sleep(5)
#在表单里面操作完毕后,无论页面是否进行跳转,必须有退出表单的操作
browser163Email.switch_to_default_content() 


#给指定邮件地址发送天气邮件
browser163Email.find_element_by_id('_mail_component_70_70').click()#点击“写信”,页面跳转到写信页面 
sleep(5)

#发送邮件地址
browserHao123_163Email.find_element_by_class_name('nui-editableAddr-ipt').clear()
browserHao123_163Email.find_element_by_class_name('nui-editableAddr-ipt').send_keys('目的邮箱')

#发送邮件主题,ebElement_*对象的列表
browserHao123_163Email.find_elements_by_class_name('nui-ipt-input')[2].clear()
browserHao123_163Email.find_elements_by_class_name('nui-ipt-input')[2].send_keys('天气预报')

#填写邮件内容,由于正文编辑存在表单,先切换表单
frame = browserHao123_163Email.find_element_by_class_name("APP-editor-iframe")#如果ifram有id或者name属性时可以直接切换
browserHao123_163Email.switch_to_frame(frame)
browserHao123_163Email.find_element_by_class_name('nui-scroll').clear()
browserHao123_163Email.find_element_by_class_name('nui-scroll').send_keys(maileNeiRong)

#在表单里面操作完毕后,无论页面是否进行跳转,必须有退出表单的操作
browserHao123_163Email.switch_to_default_content()

browserHao123_163Email.find_elements_by_class_name('nui-btn-text')[-2].click()


"""
#给指定邮件地址发送天气邮件
all_handles = browserHao123_163Email.window_handles
#多窗口,根据窗口句柄的再判断,切换到163窗口
for handle in all_handles:
    if handle != handle_hao123:
        browserHao123_163Email.switch_to_window(handle)



browserHao123_163Email.find_elements_by_class_name('oz0')[1].click()#点击“写信”,页面跳转到写信页面 
sleep(5)

#发送邮件地址
browserHao123_163Email.find_element_by_class_name('nui-editableAddr-ipt').clear()
browserHao123_163Email.find_element_by_class_name('nui-editableAddr-ipt').send_keys('目的邮箱')

#发送邮件主题,ebElement_*对象的列表
browserHao123_163Email.find_elements_by_class_name('nui-ipt-input')[2].clear()
browserHao123_163Email.find_elements_by_class_name('nui-ipt-input')[2].send_keys('天气预报')

#填写邮件内容,由于正文编辑存在表单,先切换表单
frame = browserHao123_163Email.find_element_by_class_name("APP-editor-iframe")#如果ifram有id或者name属性时可以直接切换
browserHao123_163Email.switch_to_frame(frame)
browserHao123_163Email.find_element_by_class_name('nui-scroll').clear()
browserHao123_163Email.find_element_by_class_name('nui-scroll').send_keys(maileNeiRong)

#在表单里面操作完毕后,无论页面是否进行跳转,必须有退出表单的操作
browserHao123_163Email.switch_to_default_content()

browserHao123_163Email.find_elements_by_class_name('nui-btn-text')[-2].click()


本文转载:CSDN博客