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()
#显式等待发送成功的//h2[contains(@id,"succInfo")] //h2[text()="发送成功"]
if wait.until(EC.visibility_of_element_located(('xpath','//h2[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://njwzjz.com/article/joscop.html