python+selenium 自动化测试入门示例(邮件发送)

最近在做工作交接空闲较多,看到测试的妹子(江湖人称红姐)挺忙的也刚开始学习python,所以昨天看了一个下午python和selenium,做了个测试的示例,第一写python,有很多问题还请高人指教

首先需要安装python 

进入命令行 输入python –version 查看python版本,没有没有安装的话,建议安装python3.x版本,从2.x到3.x改动很大,下载地址和安装方法百度一下很多

1、安装selenium,再命令行执行 python -m pip install selenium  

若安装成功之后,在命令行执行 python -m pydoc -p 8888   (-p 是指定端口号)  

 访问http://127.0.0.1:8888 在页面的最下面site-packages 一栏 会多出一个selenium项,

当前python下安装的包和api都能在这边查得到


2、本示例是以谷歌浏览操作的(其他浏览器大同小异),下载chromdriver驱动,驱动包放在python根目录下


3、接下来开始进入正题

1) 首先得了解邮件发送的步骤,

第一步:打开浏览器,调到登录页面,输入账号密码并登录;

第二部:点击写邮件,输入地址、标题和内容,并发送;

第三部:关闭浏览器;

2) 打开浏览器并调到登录页面:

创建main.py文件并写入

# coding = utf-8

from selenium import webdriver 

driver = webdriver.Chrome()
driver.maximize_window() #浏览器最大化
url = 'http://qiye.163.com/login/'
driver.get(url)
driver.find_element_by_id('accname').clear()
driver.find_element_by_id('accname').send_keys('123456@qq.com')
driver.find_element_by_id('accpwd').clear()
driver.find_element_by_id('accpwd').send_keys('123456')
driver.find_element_by_xpath('/html/body/section/div/div/div[2]/div[2]/form[1]/div[4]/button').click()
time.sleep(3)

点击写邮件,输入地址、标题和内容,并发送

再main.py文件开头引入依赖的包

from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

并将下面代码写在后面

    driver.switch_to.frame('folder') #切换到iframe
    driver.find_element_by_xpath('/html/body/div/div/div/table/tbody/tr[1]/td/h1/a[2]').click() #点击写信按钮
    driver.switch_to.default_content() #切到主文档
    driver.switch_to.frame('foldmain') #切换到另一个iframe
    driver.find_element_by_xpath('//*[@id="oDivTo"]/div[1]/input').send_keys('123456@qq.com') #输入地址
    driver.find_element_by_xpath('//*[@id="subject"]').send_keys('这是测试标题')   #输入标题
    driver.switch_to.frame('editor')
    driver.switch_to.frame('HtmlEditor') #找到内容区的webdriver
    driver.find_element_by_xpath('/html/body').send_keys('这是测试内容') #输入内容
    driver.switch_to.default_content() #切到主文档
    driver.switch_to.frame('foldmain') #切换到外层webdriver
    driver.find_element_by_xpath('//*[@id="oSendButton2"]').click() #点击发送
    driver.switch_to.default_content() #切到主文档
    time.sleep(1)

关闭浏览器

driver.close()

完整的main.py文件:

# coding = utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Chrome()
driver.maximize_window() #浏览器最大化
#跳转登录页并登录
url = 'http://qiye.163.com/login/'
driver.get(url)
driver.find_element_by_id('accname').clear()
driver.find_element_by_id('accname').send_keys('123456@qq.com')
driver.find_element_by_id('accpwd').clear()
driver.find_element_by_id('accpwd').send_keys('123456')
driver.find_element_by_xpath('/html/body/section/div/div/div[2]/div[2]/form[1]/div[4]/button').click()
time.sleep(3)
#填写地址标题内容等并发送
driver.switch_to.frame('folder') #切换到iframe
driver.find_element_by_xpath('/html/body/div/div/div/table/tbody/tr[1]/td/h1/a[2]').click() #点击写信按钮
driver.switch_to.default_content() #切到主文档
driver.switch_to.frame('foldmain') #切换到另一个iframe
driver.find_element_by_xpath('//*[@id="oDivTo"]/div[1]/input').send_keys('123456@qq.com') #输入地址
driver.find_element_by_xpath('//*[@id="subject"]').send_keys('这是测试标题')   #输入标题
driver.switch_to.frame('editor')
driver.switch_to.frame('HtmlEditor') #找到内容区的webdriver
driver.find_element_by_xpath('/html/body').send_keys('这是测试内容') #输入内容
driver.switch_to.default_content() #切到主文档
driver.switch_to.frame('foldmain') #切换到外层webdriver
driver.find_element_by_xpath('//*[@id="oSendButton2"]').click() #点击发送
driver.switch_to.default_content() #切到主文档
time.sleep(1)

#关闭浏览器
driver.close()

写到这里也算一个流程写完了,但是这里发送信息都是写死了而且代码写的很臃肿;
下面我数据来源改为从读取文件,代码模块化提高代码的重用性;
首先我们把登录模块单独提出来写在login.py文件里:


#coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

def login(driver):
    driver.maximize_window() #浏览器最大化
    url = 'http://qiye.163.com/login/'
    driver.get(url)
    driver.find_element_by_id('accname').clear()
    driver.find_element_by_id('accname').send_keys('123456@qq.com')
    driver.find_element_by_id('accpwd').clear()
    driver.find_element_by_id('accpwd').send_keys('123456')
    driver.find_element_by_xpath('/html/body/section/div/div/div[2]/div[2]/form[1]/div[4]/button').click()
    time.sleep(3)




将发送模块单独提出来写在send.py文件里:


#coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

def send(driver,datarow):
    driver.switch_to.frame('folder') #切换到iframe
    driver.find_element_by_xpath('/html/body/div/div/div/table/tbody/tr[1]/td/h1/a[2]').click() #点击写信按钮
    driver.switch_to.default_content() #切到主文档
    driver.switch_to.frame('foldmain') #切换到另一个iframe
    driver.find_element_by_xpath('//*[@id="oDivTo"]/div[1]/input').send_keys('123456@qq.com') #输入地址
    driver.find_element_by_xpath('//*[@id="subject"]').send_keys('这是测试标题')   #输入标题
    driver.switch_to.frame('editor')
    driver.switch_to.frame('HtmlEditor') #找到内容区的webdriver
    driver.find_element_by_xpath('/html/body').send_keys('这是测试内容') #输入内容
    driver.switch_to.default_content() #切到主文档
    driver.switch_to.frame('foldmain') #切换到外层webdriver
    driver.find_element_by_xpath('//*[@id="oSendButton2"]').click() #点击发送
    driver.switch_to.default_content() #切到主文档
    time.sleep(1)


将退出方法写在了login.py文件里,login.py文件内容如下:


	
#coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

def login(driver):
    driver.maximize_window() #浏览器最大化
    url = 'http://qiye.163.com/login/'
    driver.get(url)
    driver.find_element_by_id('accname').clear()
    driver.find_element_by_id('accname').send_keys('123456@qq.com')
    driver.find_element_by_id('accpwd').clear()
    driver.find_element_by_id('accpwd').send_keys('123456')
    driver.find_element_by_xpath('/html/body/section/div/div/div[2]/div[2]/form[1]/div[4]/button').click()
    time.sleep(3)

def loginout(driver):
    driver.close()


将这两模块提出来之后main.py只是引用和调用:


# coding = utf-8

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time,login,send

driver = webdriver.Chrome()
login.login(driver)
send.send(driver)

time.sleep(3)
login.loginout(driver)

将login和send模块引入
这里说明下,调用方法必须要传入webdriver对象传入


到这里我们就已经将代码模块化了,接下来我们再见发送文本从文件中读取:
这里我们用的python里面csv模块读取文件
将excel文件保存为emails.csv 格式为csv




csv模块能将文件每行读取为二维数组,方便后面调用


将main.py修改为:

# coding = utf-8

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time,login,send,csv,os

driver = webdriver.Chrome()
login.login(driver)

filename = os.path.dirname(os.sys.argv[0])+'\\emails.csv' #csv文件路径
print(filename)
data = []
try:
    with open(filename) as f:
        reader = csv.reader(f)
        header = next(reader)
        data = [row for row in reader]
except csv.Error as e:
    print("Error reading CSV file at line %s: %s"%(reader.line_num, e))

if header:
    print(header)
    print("=====================")
for datarow in data:
    send.send(driver,datarow) #遍历发送邮件

time.sleep(3)
login.loginout(driver)



 从emails.csv读出数据 遍历 调用send方法  并将遍历当前对象传送给send方法


当然,相应的send.py的send方法也得需要修改,上面说过csv读出来的数据是一个二维数组 所以下面调用参数改为读取数组下标:


#coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

def send(driver,datarow):
    driver.switch_to.frame('folder') #切换到iframe
    driver.find_element_by_xpath('/html/body/div/div/div/table/tbody/tr[1]/td/h1/a[2]').click() #点击写信按钮
    driver.switch_to.default_content() #切到主文档
    driver.switch_to.frame('foldmain') #切换到另一个iframe
    driver.find_element_by_xpath('//*[@id="oDivTo"]/div[1]/input').send_keys(datarow[0]) #输入地址
    driver.find_element_by_xpath('//*[@id="subject"]').send_keys(datarow[1])   #输入标题
    driver.switch_to.frame('editor')
    driver.switch_to.frame('HtmlEditor') #找到内容区的webdriver
    driver.find_element_by_xpath('/html/body').send_keys(datarow[2]) #输入内容
    driver.switch_to.default_content() #切到主文档
    driver.switch_to.frame('foldmain') #切换到外层webdriver
    driver.find_element_by_xpath('//*[@id="oSendButton2"]').click() #点击发送
    driver.switch_to.default_content() #切到主文档
    time.sleep(1)


到这里就算所有的都完成了,最后在将最终的文件展示出来
主函数main.py:


# coding = utf-8

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time,login,send,csv,os

driver = webdriver.Chrome()
login.login(driver)

filename = os.path.dirname(os.sys.argv[0])+'\\emails.csv' #csv文件路径
print(filename)
data = []
try:
    with open(filename) as f:
        reader = csv.reader(f)
        header = next(reader)
        data = [row for row in reader]
except csv.Error as e:
    print("Error reading CSV file at line %s: %s"%(reader.line_num, e))

if header:
    print(header)
    print("=====================")
for datarow in data:
    send.send(driver,datarow) #遍历发送邮件

time.sleep(3)
login.loginout(driver)


登录模块login.py:


#coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

def login(driver):
    driver.maximize_window() #浏览器最大化
    url = 'http://qiye.163.com/login/'
    driver.get(url)
    driver.find_element_by_id('accname').clear()
    driver.find_element_by_id('accname').send_keys('123456@qq.com')
    driver.find_element_by_id('accpwd').clear()
    driver.find_element_by_id('accpwd').send_keys('123456')
    driver.find_element_by_xpath('/html/body/section/div/div/div[2]/div[2]/form[1]/div[4]/button').click()
    time.sleep(3)

def loginout(driver):
    driver.close()



发送邮件send.py:


#coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

def send(driver,datarow):
    driver.switch_to.frame('folder') #切换到iframe
    driver.find_element_by_xpath('/html/body/div/div/div/table/tbody/tr[1]/td/h1/a[2]').click() #点击写信按钮
    driver.switch_to.default_content() #切到主文档
    driver.switch_to.frame('foldmain') #切换到另一个iframe
    driver.find_element_by_xpath('//*[@id="oDivTo"]/div[1]/input').send_keys(datarow[0]) #输入地址
    driver.find_element_by_xpath('//*[@id="subject"]').send_keys(datarow[1])   #输入标题
    driver.switch_to.frame('editor')
    driver.switch_to.frame('HtmlEditor') #找到内容区的webdriver
    driver.find_element_by_xpath('/html/body').send_keys(datarow[2]) #输入内容
    driver.switch_to.default_content() #切到主文档
    driver.switch_to.frame('foldmain') #切换到外层webdriver
    driver.find_element_by_xpath('//*[@id="oSendButton2"]').click() #点击发送
    driver.switch_to.default_content() #切到主文档
    time.sleep(1)




emails.csv文件截图:



  • 15
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值