最近在做工作交接空閒較多,看到測試的妹子(江湖人稱紅姐)挺忙的也剛開始學習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)
將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)
當然,相應的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('[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)