最近在做工作交接空闲较多,看到测试的妹子(江湖人称红姐)挺忙的也刚开始学习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('[email protected]')
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('[email protected]') #输入地址
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('[email protected]')
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('[email protected]') #输入地址
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('[email protected]')
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('[email protected]') #输入地址
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('[email protected]')
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()
# 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)
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)
#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)
# 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('[email protected]')
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)