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_conditions as ECimport time

#print u"启动浏览器..."print "start browser..."#创建Firefox浏览器实例driver=webdriver.Firefox(executable_path="c:\\geckodriver")#最大化浏览器窗口driver.maximize_window()#print u"启动浏览器成功..."print "start browser done..."#print u"访问126邮箱登页。。。"print "access 126 mail login page..."driver.get("http://mail.126.com")#暂停5秒钟,一遍邮箱登录页面加载完成time.sleep(5)assert u"126网易免费邮--你的专业电子邮局" in driver.title

print "access 126 mail login page done"

wait=WebDriverWait(driver,30)

wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"x-URS-iframe")))

username=driver.find_element_by_xpath("//input[@name=‘email‘]")

username.send_keys("xiaxiaoxu1987")

pwd=driver.find_element_by_xpath("//input[@name=‘password‘]")

pwd.send_keys("gloryroad")

pwd.send_keys(Keys.RETURN)

print "user login..."

time.sleep(5)time1=time.time()driver.switch_to.default_content()print "total time:",time.time()-time1assert u"网易邮箱" in driver.titleprint "login done"address_book_link = wait.until(lambda x: x.find_element_by_xpath("//div[text()=‘通讯录‘]"))address_book_link.click()

add_contact_button = wait.until(lambda x: x.find_element_by_xpath("//span[text()=‘新建联系人‘]"))add_contact_button.click()

contact_name = wait.until(lambda x: x.find_element_by_xpath("//a[@title=‘编辑详细姓名‘]/preceding-sibling::div/input"))contact_name.send_keys(u"徐凤钗")contact_email = wait.until(lambda x: x.find_element_by_xpath("//*[@id=‘iaddress_MAIL_wrap‘]//input"))contact_email.send_keys("[email protected]")contact_is_star = wait.until(lambda x: x.find_element_by_xpath("//span[text()=‘设为星标联系人‘]/preceding-sibling::span/b"))contact_is_star.click()contact_mobile = wait.until(lambda x: x.find_element_by_xpath("//*[@id=‘iaddress_TEL_wrap‘]//dd//input"))contact_mobile.send_keys(‘18141134488‘)contact_other_info = wait.until(lambda x: x.find_element_by_xpath("//textarea"))contact_other_info.send_keys(‘my wife‘)contact_save_button = wait.until(lambda x: x.find_element_by_xpath("//span[.=‘确 定‘]"))contact_save_button.click()

print u"进入首页。。。"time.sleep(3)mainPage=wait.until(EC.visibility_of_element_located((By.XPATH,"//div[.=‘首页‘]")))#也好用#mainPage=wait.until(lambda x: x.find_element(by=‘xpath‘, value = "//div[.=‘首页‘]"))#好用#mainPage=driver.find_element_by_xpath("//div[.=‘首页‘]")#好用mainPage.click()assert u"已发送" in driver.page_sourceprint u"进入首页成功"print "write message..."writeMessage=wait.until(lambda x:x.find_element_by_xpath("//span[text()=‘写 信‘]"))writeMessage.click()#收件人receiver=wait.until(lambda x:x.find_element_by_xpath("//div[contains(@id,‘_mail_emailinput‘)]/input"))receiver.send_keys("[email protected]")#主题theme=wait.until(lambda x:x.find_element_by_xpath("//div[@aria-label=‘邮件主题输入框,请输入邮件主题‘]/input"))theme.send_keys(u"测试邮件")#添加附件attachment=wait.until(lambda x:x.find_element_by_xpath("//div[@title=‘点击添加附件‘]/input[@size=‘1‘ and @type=‘file‘]"))attachment.send_keys("d:\\test.txt")#切入正文iframedriver.switch_to.frame(driver.find_element_by_xpath("//iframe[@tabindex=1]"))

editBox=driver.find_element_by_xpath(‘/html/body‘)editBox.send_keys(u"发给夏晓旭的一封信")driver.switch_to.default_content()#print u"写信完成"print "write message done"driver.find_element_by_xpath("//header//span[text()=‘发送‘]").click()#print u"开始发送邮件"print "start to send email.."time.sleep(3)assert u"发送成功" in driver.page_source#print u"邮件发送成功"print "send emial done"driver.quit()

结果:

C:\Python27\python.exe D:/test/hybrid_version2/TestScripts/TestScript.py
start browser...
start browser done...
access 126 mail login page...
access 126 mail login page done
user login...
total time: 0.00800013542175
login done
进入首页。。。
进入首页成功
write message...
write message done
start to send email..
send emial done

Process finished with exit code 0



原文地址:https://www.cnblogs.com/xiaxiaoxu/p/9343935.html

时间: 2024-08-28 18:11:13

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

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

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

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方式定

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

转自:https://www.cnblogs.com/wuhl-89/p/7778463.html 查看元素发现id为动态,所以不选择以id定位. 使用xpath路径定位,每次获取元素都失败,最后网上查了下发现其嵌套在表单frame/iframe中,所以我们先要进入frame/iframe中,然后再定位 frame/iframe的定位方式有3种,一是通过id:二是通过name:三是xpath 例:switch_to_frame(id)  : 或是  switch_to_frame(name):若

Python WebDriver + Firefox 文件下载

firefox可以通过 在地址栏输入:about:config 或about:aupport 来查看或修改配置信息. 这里有两种解决方式, 1.设置自动保存下载 如下图勾选:以后自动采用相同的动作处理此类文件 这样下次在下载该类型的文件时就不会这样提醒了. 如果想修改设置可以在 浏览器选项中进行修改 如下图 这样设置完成后,但是程序启动时打开的浏览器并没有按照这种配置打开. 对比下图可以发现,通过webdriver打开的浏览器与手工打开的浏览器展示的不同,这是因为webdriver打开的浏览器没

Python - WebDriver 识别登录验证码

没什么可说的直接上代码! #-*-coding:utf-8-*- # Time:2017/9/29 7:16 # Author:YangYangJun import time from pytesseract import * from selenium import webdriver from PIL import Image, ImageEnhance import baseinfo url = baseinfo.url driver = webdriver.Firefox() drive

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 TimeoutExceptio

python webdriver 一步一步搭建数据驱动测试框架的过程和总结

一步一步搭建数据驱动测试框架的过程和总结数据驱动框架结构:Action:    封装的操作元素的函数,如login,添加联系人... conf:日志配置文件定位元素配置文件数据库配置文件    PageObject:    一个页面是一个类,类的方法可以获取页面上的相关元素 ProjectVar:工程路径工程相关的全局变量TestData:(文件或excel)测试用例测试数据 TestScript:运行测试框架的主程序:入口,主要读取测试数据的文件                      记录

python发送邮件和附件

发送邮件的时候,需要发送人,收件人,和一台邮件服务器,这里使用python发送一个邮件,主要需要引入smtplib和email库.下面是源码,粘贴即可用: #!/usr/bin/env python3 # coding: utf-8 import smtplib import time import sys from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart # 发送邮件 def