python Selenium 庫的使用技巧
Selenium 是一個用于Web應用程序測試的工具。Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣。支持的瀏覽器包括IE,Mozilla Firefox,Safari,Google Chrome,Opera等。這個工具的主要功能包括:測試與瀏覽器的兼容性——測試你的應用程序看是否能夠很好得工作在不同瀏覽器和操作系統(tǒng)之上。測試系統(tǒng)功能——創(chuàng)建回歸測試檢驗軟件功能和用戶需求。支持自動錄制動作和自動生成 .Net、Java、Perl等不同語言的測試腳本。 -- 百度百科
首先下載驅動文件:https://chromedriver.storage.googleapis.com/index.html?path=2.39/
放入google目錄下

測試代碼,測試是否能讀取到驅動文件。
from selenium import webdriver path = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe" driver = webdriver.Chrome(executable_path=path) url = "https://www.baidu.com" driver.get(url) print(driver.page_source)

簡單的實現(xiàn)瀏覽器測試
# -*- coding:utf-8 -*-
from selenium import webdriver
WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)
driver.set_window_size(1000,500)
url = "https://www.baidu.com"
driver.get(url)
print(driver.find_element_by_id("kw"))
Selenium 自動化測試庫的使用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="gbk">
<title>Selenium Test</title>
</head>
<body>
<div class="acount" id="aid">
<a class="mnav" rel="external nofollow" name="trnews">新聞</a>
<a class="mnav" rel="external nofollow" name="myblog">我的博客</a>
<a class="mnav" rel="external nofollow" name="mygit">GitHub</a>
</div>
<form id="forms" class="fms" name="submit_form" action="index.html">
<span class="soutu-btn"></span>
<p>用戶: <input id="user" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off"></p>
<p>密碼: <input id="pass" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off"></p>
<input type="submit" value="提交" />
</form>
<p name="p1" > hello lyshark p1</p>
<p name="p2" > hello lyshark p2</p>
</body>
</html>
通過簡單的瀏覽文件并實現(xiàn)簡單的定位.
# 驅動下載地址: http://chromedriver.storage.googleapis.com/index.html
from selenium import webdriver
WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)
driver.set_window_size(1024,768)
# 常用的定位變量參數(shù)如下所示.
driver.get("http://lyshark.com")
print("當前URL: {}".format(driver.current_url))
print("當前標題: {}".format(driver.title))
print("網(wǎng)頁代碼: {}".format(driver.page_source))
# 基本的 find_element 標簽查找定位方式
print(driver.find_element_by_id("user")) # 通過ID來查找元素
print(driver.find_element_by_name("p1").text) # 通過name屬性來定位
print(driver.find_element_by_class_name("s_ipt")) # 通過類名來定位
# 通過xpath定位,xpath定位有N種寫法,這里列幾個常用寫法
print(driver.find_element_by_xpath("http://form[@class='fms']//input[@id='user']"))
print(driver.find_element_by_xpath("http://p[@name='p1']"))
print(driver.find_element_by_xpath("http://html/body/form/p/input"))
print(driver.find_elements_by_css_selector(".fms #user"))
# 定位a標簽中的關鍵字.
print(driver.find_element_by_link_text("新聞"))
print(driver.find_element_by_partial_link_text("我"))
通過xpath定位標簽并自動輸入內容,發(fā)送登錄請求到后端,寫法如下.
from selenium import webdriver
WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)
driver.set_window_size(1024,768)
driver.get("http://lyshark.com")
# 通過xpath語法定位到用戶名的標簽上并且自動輸入lyshark這個用戶名
driver.find_element_by_xpath("http://form[@class='fms']/p//input[@id='user']").send_keys("lyshark")
# 通過xpath語法定位到密碼的標簽上清空默認值,然后輸入123123密碼
driver.find_element_by_xpath("http://form[@class='fms']/p//input[@id='pass']").clear()
driver.find_element_by_xpath("http://form[@class='fms']/p//input[@id='pass']").send_keys("123123")
# 提交這個請求,默認有兩種提交方式一種是 click() 一種是submit()
driver.find_element_by_xpath("http://form[@class='fms']/input[@type='submit']").click()
通過鍵盤鼠標類庫記錄并可回放
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)
driver.set_window_size(1024,768)
driver.get("https://www.baidu.com")
# ------------------------------------------------------------------------
# ActionChains 類提供了鼠標操作的常用方法,鼠標事件的常用函數(shù)說明
# perform(): 鼠標懸浮于標簽
# context_click(): 右擊
# double_click(): 雙擊
# drag_and_drop(): 拖動
# move_to_element():鼠標懸停
# 定位到要懸停的元素
above = driver.find_element_by_link_text("更多產品")
# 對定位到的元素執(zhí)行鼠標懸停操作
ActionChains(driver).move_to_element(above).perform()
# ------------------------------------------------------------------------
# webdriver.common.keys 類提供了鍵盤事件的操作,以下為常用的鍵盤操作:
# send_keys(Keys.BACK_SPACE) 刪除鍵(BackSpace)
# send_keys(Keys.SPACE) 空格鍵(Space)
# send_keys(Keys.TAB) 制表鍵(Tab)
# send_keys(Keys.ESCAPE) 回退鍵(Esc)
# send_keys(Keys.ENTER) 回車鍵(Enter)
# send_keys(Keys.CONTROL,'a') 全選(Ctrl+A)
# send_keys(Keys.CONTROL,'c') 復制(Ctrl+C)
# send_keys(Keys.CONTROL,'x') 剪切(Ctrl+X)
# send_keys(Keys.CONTROL,'v') 粘貼(Ctrl+V)
# send_keys(Keys.F1) 鍵盤 F1
# 輸入框輸入內容
driver.find_element_by_id("kw").send_keys("seleniumm")
# 刪除多輸入的一個 m
driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)
# 輸入空格鍵+從入門到入土
driver.find_element_by_id("kw").send_keys(Keys.SPACE)
driver.find_element_by_id("kw").send_keys("從入門到入土")
# ctrl+a 全選輸入框內容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'a')
# ctrl+x 剪切輸入框內容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'x')
# ctrl+v 粘貼內容到輸入框
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'v')
# 通過回車鍵來代替單擊操作
driver.find_element_by_id("su").send_keys(Keys.ENTER)
簡單的點擊事件
# -*- coding:utf-8 -*-
from selenium import webdriver
import time
WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)
driver.set_window_size(1024,768)
driver.get("https://www.baidu.com")
driver.find_element_by_id("kw").send_keys("lyshark") # 發(fā)送給id=kw的編輯框,搜索關鍵字 lyshark
driver.find_element_by_id("su").click() # 點擊搜索按鈕,百度一下的ID是su
time.sleep(1)
# xpath 語法 尋找 div id是1里面的 a標簽取出標簽中的 contains text()
driver.find_element_by_xpath("http://div[@id='1']//a[contains(text(),'-')]").click()
time.sleep(1)
handle = driver.current_window_handle # 獲取當前窗口句柄
handle_all = driver.window_handles # 獲取當前所有開啟窗口的句柄
print(handle_all)
driver.switch_to.window(handle_all[0]) # 切換到第一個窗口中
time.sleep(1)
driver.find_element_by_id("kw").clear() # 接著清空搜索框中的內容

百度自動收集
from selenium import webdriver
from bs4 import BeautifulSoup
from queue import Queue
import requests,os,re,lxml
# driver: http://chromedriver.storage.googleapis.com/index.html?path=79.0.3945.36/
head = {"User-Agent":"Mozilla/5.0 (iPhone; U; CPU like Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3"}
WebPath = "C:/Users/LyShark/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(executable_path=WebPath)
queue = Queue()
for item in range(0,1000,10):
queue.put('https://www.baidu.com/s?wd={}&pn={}'.format("lyshark",str(item)))
for item in queue.queue:
driver.get(item)
ret = str(driver.page_source)
try:
soup = BeautifulSoup(ret,'lxml')
urls = soup.find_all(name='a',attrs={'data-click':re.compile(('.')),'class':None})
for item in urls:
get_url = requests.get(url=item['href'],headers=head,timeout=5)
if get_url.status_code == 200:
print(get_url.url)
except Exception:
pass

頁面等待
from selenium import webdriver
driver=webdriver.Chrome()
driver.get('https://www.taobao.com/')
wait=WebDriverWait(driver,3) #設置監(jiān)聽driver等待時間3秒
input=wait.until(EC.presence_of_element_located((By.ID,'q'))) #設置等待條件為id為q的元素加載完成
button=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn-search'))) #設置等待條件為class名為btn-search的元素加載完成
print(input,button)
driver = webdriver.Firefox()
driver.implicitly_wait(10) #隱式等待設置為10等待時間
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
鍵盤操作
element=driver.find_element_by_id('search') #獲取輸入框
element.send_keys('selenium') #搜索selenium包
element.send_keys(Keys.ENTER) #按回車鍵
element_a=driver.find_element_by_link_text('selenium') #定位selenium包鏈接
ActionChains(driver).move_to_element(element_a).click(element_a).perform() #按左鍵點擊鏈接執(zhí)行
element_down=driver.find_element_by_link_text('Download files') #定位下載鏈接
ActionChains(driver).move_to_element(element_down).click(element_down).perform() #按左鍵點擊鏈接
element_selenium=driver.find_element_by_link_text('selenium-3.13.0.tar.gz') #定位元素selenium下載包鏈接
data=element_selenium.get_attribute('href') #獲取鏈接地址
with open('selenium-3.13.0.tar.gz','wb') as f:
source=requests.get(data).content #請求下載鏈接地址獲取二進制包數(shù)據(jù)
f.write(source) #寫入數(shù)據(jù)
f.close()
driver.quit()
menu = driver.find_element_by_css_selector(".nav") #獲取element對象
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1") #獲取點擊對象
#創(chuàng)建鼠標對象
actions = ActionChains(driver)
#移動鼠標到對象
actions.move_to_element(menu)
#點擊對象
actions.click(hidden_submenu)
#執(zhí)行操作
actions.perform()
文章作者:lyshark
文章出處:https://www.cnblogs.com/lyshark
以上就是python Selenium 庫的使用技巧的詳細內容,更多關于python Selenium 庫的資料請關注腳本之家其它相關文章!
相關文章
使用TensorFlow對圖像進行隨機旋轉的實現(xiàn)示例
這篇文章主要介紹了使用TensorFlow對圖像進行隨機旋轉的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01
python 在指定范圍內隨機生成不重復的n個數(shù)實例
今天小編就為大家分享一篇python 在指定范圍內隨機生成不重復的n個數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python3編程實現(xiàn)獲取阿里云ECS實例及監(jiān)控的方法
這篇文章主要介紹了Python3編程實現(xiàn)獲取阿里云ECS實例及監(jiān)控的方法,涉及Python URL登陸及請求處理相關操作技巧,需要的朋友可以參考下2017-08-08

