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

測(cè)試代碼,測(cè)試是否能讀取到驅(qū)動(dòng)文件。
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)

簡(jiǎn)單的實(shí)現(xiàn)瀏覽器測(cè)試
# -*- 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 自動(dòng)化測(cè)試庫(kù)的使用:
<!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>
通過(guò)簡(jiǎn)單的瀏覽文件并實(shí)現(xiàn)簡(jiǎn)單的定位.
# 驅(qū)動(dòng)下載地址: 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("當(dāng)前URL: {}".format(driver.current_url))
print("當(dāng)前標(biāo)題: {}".format(driver.title))
print("網(wǎng)頁(yè)代碼: {}".format(driver.page_source))
# 基本的 find_element 標(biāo)簽查找定位方式
print(driver.find_element_by_id("user")) # 通過(guò)ID來(lái)查找元素
print(driver.find_element_by_name("p1").text) # 通過(guò)name屬性來(lái)定位
print(driver.find_element_by_class_name("s_ipt")) # 通過(guò)類(lèi)名來(lái)定位
# 通過(guò)xpath定位,xpath定位有N種寫(xiě)法,這里列幾個(gè)常用寫(xiě)法
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標(biāo)簽中的關(guān)鍵字.
print(driver.find_element_by_link_text("新聞"))
print(driver.find_element_by_partial_link_text("我"))
通過(guò)xpath定位標(biāo)簽并自動(dòng)輸入內(nèi)容,發(fā)送登錄請(qǐng)求到后端,寫(xiě)法如下.
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")
# 通過(guò)xpath語(yǔ)法定位到用戶名的標(biāo)簽上并且自動(dòng)輸入lyshark這個(gè)用戶名
driver.find_element_by_xpath("http://form[@class='fms']/p//input[@id='user']").send_keys("lyshark")
# 通過(guò)xpath語(yǔ)法定位到密碼的標(biāo)簽上清空默認(rèn)值,然后輸入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")
# 提交這個(gè)請(qǐng)求,默認(rèn)有兩種提交方式一種是 click() 一種是submit()
driver.find_element_by_xpath("http://form[@class='fms']/input[@type='submit']").click()
通過(guò)鍵盤(pán)鼠標(biāo)類(lèi)庫(kù)記錄并可回放
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 類(lèi)提供了鼠標(biāo)操作的常用方法,鼠標(biāo)事件的常用函數(shù)說(shuō)明
# perform(): 鼠標(biāo)懸浮于標(biāo)簽
# context_click(): 右擊
# double_click(): 雙擊
# drag_and_drop(): 拖動(dòng)
# move_to_element():鼠標(biāo)懸停
# 定位到要懸停的元素
above = driver.find_element_by_link_text("更多產(chǎn)品")
# 對(duì)定位到的元素執(zhí)行鼠標(biāo)懸停操作
ActionChains(driver).move_to_element(above).perform()
# ------------------------------------------------------------------------
# webdriver.common.keys 類(lèi)提供了鍵盤(pán)事件的操作,以下為常用的鍵盤(pán)操作:
# 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) 回車(chē)鍵(Enter)
# send_keys(Keys.CONTROL,'a') 全選(Ctrl+A)
# send_keys(Keys.CONTROL,'c') 復(fù)制(Ctrl+C)
# send_keys(Keys.CONTROL,'x') 剪切(Ctrl+X)
# send_keys(Keys.CONTROL,'v') 粘貼(Ctrl+V)
# send_keys(Keys.F1) 鍵盤(pán) F1
# 輸入框輸入內(nèi)容
driver.find_element_by_id("kw").send_keys("seleniumm")
# 刪除多輸入的一個(gè) m
driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)
# 輸入空格鍵+從入門(mén)到入土
driver.find_element_by_id("kw").send_keys(Keys.SPACE)
driver.find_element_by_id("kw").send_keys("從入門(mén)到入土")
# ctrl+a 全選輸入框內(nèi)容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'a')
# ctrl+x 剪切輸入框內(nèi)容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'x')
# ctrl+v 粘貼內(nèi)容到輸入框
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'v')
# 通過(guò)回車(chē)鍵來(lái)代替單擊操作
driver.find_element_by_id("su").send_keys(Keys.ENTER)
簡(jiǎn)單的點(diǎn)擊事件
# -*- 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的編輯框,搜索關(guān)鍵字 lyshark
driver.find_element_by_id("su").click() # 點(diǎn)擊搜索按鈕,百度一下的ID是su
time.sleep(1)
# xpath 語(yǔ)法 尋找 div id是1里面的 a標(biāo)簽取出標(biāo)簽中的 contains text()
driver.find_element_by_xpath("http://div[@id='1']//a[contains(text(),'-')]").click()
time.sleep(1)
handle = driver.current_window_handle # 獲取當(dāng)前窗口句柄
handle_all = driver.window_handles # 獲取當(dāng)前所有開(kāi)啟窗口的句柄
print(handle_all)
driver.switch_to.window(handle_all[0]) # 切換到第一個(gè)窗口中
time.sleep(1)
driver.find_element_by_id("kw").clear() # 接著清空搜索框中的內(nèi)容

百度自動(dòng)收集
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

頁(yè)面等待
from selenium import webdriver
driver=webdriver.Chrome()
driver.get('https://www.taobao.com/')
wait=WebDriverWait(driver,3) #設(shè)置監(jiān)聽(tīng)driver等待時(shí)間3秒
input=wait.until(EC.presence_of_element_located((By.ID,'q'))) #設(shè)置等待條件為id為q的元素加載完成
button=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn-search'))) #設(shè)置等待條件為class名為btn-search的元素加載完成
print(input,button)
driver = webdriver.Firefox()
driver.implicitly_wait(10) #隱式等待設(shè)置為10等待時(shí)間
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
鍵盤(pán)操作
element=driver.find_element_by_id('search') #獲取輸入框
element.send_keys('selenium') #搜索selenium包
element.send_keys(Keys.ENTER) #按回車(chē)鍵
element_a=driver.find_element_by_link_text('selenium') #定位selenium包鏈接
ActionChains(driver).move_to_element(element_a).click(element_a).perform() #按左鍵點(diǎn)擊鏈接執(zhí)行
element_down=driver.find_element_by_link_text('Download files') #定位下載鏈接
ActionChains(driver).move_to_element(element_down).click(element_down).perform() #按左鍵點(diǎn)擊鏈接
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 #請(qǐng)求下載鏈接地址獲取二進(jìn)制包數(shù)據(jù)
f.write(source) #寫(xiě)入數(shù)據(jù)
f.close()
driver.quit()
menu = driver.find_element_by_css_selector(".nav") #獲取element對(duì)象
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1") #獲取點(diǎn)擊對(duì)象
#創(chuàng)建鼠標(biāo)對(duì)象
actions = ActionChains(driver)
#移動(dòng)鼠標(biāo)到對(duì)象
actions.move_to_element(menu)
#點(diǎn)擊對(duì)象
actions.click(hidden_submenu)
#執(zhí)行操作
actions.perform()
文章作者:lyshark
文章出處:https://www.cnblogs.com/lyshark
以上就是python Selenium 庫(kù)的使用技巧的詳細(xì)內(nèi)容,更多關(guān)于python Selenium 庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
深入理解Python虛擬機(jī)中魔術(shù)方法的使用
這篇文章主要給大家介紹在?cpython?當(dāng)中一些比較花里胡哨的魔術(shù)方法,以幫助我們自己實(shí)現(xiàn)比較花哨的功能,當(dāng)然這其中也包含一些也非常實(shí)用的魔術(shù)方法,需要的可以參考下2023-05-05
使用TensorFlow對(duì)圖像進(jìn)行隨機(jī)旋轉(zhuǎn)的實(shí)現(xiàn)示例
這篇文章主要介紹了使用TensorFlow對(duì)圖像進(jìn)行隨機(jī)旋轉(zhuǎn)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
python編程學(xué)習(xí)np.float 被刪除的問(wèn)題解析
這篇文章主要為大家介紹了python編程學(xué)習(xí)np.float 被刪除的問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
150行python代碼實(shí)現(xiàn)貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了150行python代碼實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
Python繪制分形圖案探索無(wú)限細(xì)節(jié)和奇妙之美
本文將介紹如何使用Python繪制各種分形圖案,包括分形樹(shù)、科赫曲線、曼德博集合等。通過(guò)本文讀者可以了解分形圖案的基本概念和構(gòu)造方法,并學(xué)會(huì)使用Python繪制出各種精美的分形圖案。本文還提供了具體的代碼示例和實(shí)踐案例,幫助讀者更好地理解分形圖案的奇妙之美2023-04-04
python 在指定范圍內(nèi)隨機(jī)生成不重復(fù)的n個(gè)數(shù)實(shí)例
今天小編就為大家分享一篇python 在指定范圍內(nèi)隨機(jī)生成不重復(fù)的n個(gè)數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python3編程實(shí)現(xiàn)獲取阿里云ECS實(shí)例及監(jiān)控的方法
這篇文章主要介紹了Python3編程實(shí)現(xiàn)獲取阿里云ECS實(shí)例及監(jiān)控的方法,涉及Python URL登陸及請(qǐng)求處理相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Python中的類(lèi)的定義和對(duì)象的創(chuàng)建方法
object?是?Python?為所有對(duì)象提供的?基類(lèi),提供有一些內(nèi)置的屬性和方法,可以使用?dir?函數(shù)查看,這篇文章主要介紹了Python中的類(lèi)的定義和對(duì)象的創(chuàng)建,需要的朋友可以參考下2022-11-11

