300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > python+selenium自动登录126邮箱并发送邮件

python+selenium自动登录126邮箱并发送邮件

时间:2021-05-15 12:18:34

相关推荐

python+selenium自动登录126邮箱并发送邮件

# 代码拿来就能使用,只需更改账号、密码和收件人邮箱;未封装import timefrom selenium import webdriverfrom mon.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECurl="/"driver=webdriver.Chrome(executable_path="d:\\chromedriver.exe")driver.implicitly_wait(10) # 隐式等待driver.get(url)wait=WebDriverWait(driver,10,0.2) # 显示等待"切换iframe"wait.until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_xpath\('//iframe[contains(@id,"x-URS-iframe")]')))"定位账号框输入"input=wait.until(EC.visibility_of(driver.find_element_by_xpath('//input[@name="email"]')))input.clear()input.send_keys(账号)"定位密码框输入"passwd=wait.until(EC.visibility_of(driver.find_element_by_xpath('//input[@name="password"]')))passwd.clear()passwd.send_keys(密码)"定位登陆按钮"wait.until(EC.element_to_be_clickable((By.XPATH,'//a[@id="dologin"]'))).click()#wait.until(EC.element_to_be_clickable((By.ID,"dologin"))).click()logout=wait.until(EC.element_to_be_clickable((By.XPATH,'//a[.="退出"]')))if logout:print ("邮箱登陆成功")"点击写信"wait.until(EC.element_to_be_clickable((By.XPATH,'//li[contains(@id,"_mail_component")]//span[.="写 信"]'))).click()"输入收件人"addressee=wait.until(EC.visibility_of(driver.find_element_by_xpath('//input[@class="nui-editableAddr-ipt"]')))addressee.send_keys(收件人邮箱)"输入主题"theme=wait.until(EC.visibility_of(driver.find_element_by_xpath('//input[contains(@id,"subjectInput")]')))theme.send_keys("测试邮件")"上传附件"upload=driver.find_element_by_xpath('//input[@type="file"]')upload.send_keys(附件的绝对路径)"切换iframe,写入文件内容"driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@class="APP-editor-iframe"]'))driver.find_element_by_xpath('//body[@class="nui-scroll"]').send_keys("测试邮件内容,请查收")driver.switch_to.default_content()"点击发送"driver.find_element_by_xpath('//footer[contains(@class,"jp")]/div[@role="button"]/span[.="发送"]').click()"断言是否发送成功"assert "发送成功" in driver.page_source,"发送失败"print ("邮件发送成功")"强制等待查看效果"time.sleep(2)"关闭浏览器"driver.quit()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。