前言:
??????個(gè)人簡(jiǎn)介:以山河作禮。
??????:Python領(lǐng)域新星創(chuàng)作者,CSDN實(shí)力新星認(rèn)證
?????第一篇文章《1.認(rèn)識(shí)網(wǎng)絡(luò)爬蟲(chóng)》獲得全站熱榜第一,python領(lǐng)域熱榜第一
。
?? ??第四篇文章《4.網(wǎng)絡(luò)爬蟲(chóng)—Post請(qǐng)求(實(shí)戰(zhàn)演示)》全站熱榜第八
。
?? ??第八篇文章《8.網(wǎng)絡(luò)爬蟲(chóng)—正則表達(dá)式RE實(shí)戰(zhàn)》全站熱榜第十二
。
?? ??第十篇文章《10.網(wǎng)絡(luò)爬蟲(chóng)—MongoDB詳講與實(shí)戰(zhàn)》全站熱榜第八,領(lǐng)域熱榜第二
?? ??第十三篇文章《13.網(wǎng)絡(luò)爬蟲(chóng)—多進(jìn)程詳講(實(shí)戰(zhàn)演示)》全站熱榜第十二
。
????《Python網(wǎng)絡(luò)爬蟲(chóng)》專欄累計(jì)發(fā)表十三篇文章,上榜五篇。歡迎免費(fèi)訂閱!歡迎大家一起學(xué)習(xí),一起成長(zhǎng)?。?br> ????悲索之人烈焰加身,墮落者不可饒恕。永恒燃燒的羽翼,帶我脫離凡間的沉淪。
一·selenium簡(jiǎn)介
?? ??Selenium是一個(gè)自動(dòng)化測(cè)試工具,用于測(cè)試Web應(yīng)用程序。它可以模擬用戶在Web瀏覽器中的操作,如點(diǎn)擊鏈接、填寫(xiě)表單、提交表單等。
- Selenium的主要特點(diǎn)是靈活性和可擴(kuò)展性,它可以與其他工具和框架集成,如
JUnit
、TestNG
、Maven
、Ant
等。 - Selenium的核心組件包括
Selenium IDE
、Selenium WebDriver
和Selenium Grid
。 - Selenium IDE是一個(gè)瀏覽器插件,用于錄制和回放測(cè)試腳本`;
- Selenium WebDriver是一個(gè)自動(dòng)化測(cè)試框架,
用于編寫(xiě)和執(zhí)行測(cè)試腳本
- Selenium Grid是一個(gè)分布式測(cè)試框架,
用于在多臺(tái)計(jì)算機(jī)上并行執(zhí)行測(cè)試腳本
。 - Selenium在Web應(yīng)用程序測(cè)試領(lǐng)域具有廣泛的應(yīng)用和影響力。
Selenium 的優(yōu)點(diǎn)
1.跨平臺(tái)
:Selenium 可以在多種操作系統(tǒng)和瀏覽器上運(yùn)行,可以在不同的環(huán)境中進(jìn)行測(cè)試。
2. 靈活性
:Selenium 提供了多種 API 和工具,可以根據(jù)具體需求進(jìn)行定制化開(kāi)發(fā),滿足不同的測(cè)試需求。
3. 易于學(xué)習(xí)
:Selenium 的 API 簡(jiǎn)單易懂,學(xué)習(xí)成本較低,而且有豐富的文檔和社區(qū)支持
4. 可擴(kuò)展性
:Selenium 可以與其他測(cè)試工具和框架集成,例如 TestNG、JUnit 等,從而實(shí)現(xiàn)更加完善的測(cè)試流程。
二·安裝模塊
?? 在pycharm的終端輸入安裝模塊的命令:
pip install selenium
可以使用清華源安裝加快安裝速度:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple selenium
三·設(shè)置瀏覽器驅(qū)動(dòng)
確認(rèn)版本:
?? 博主使用的是谷歌瀏覽器,確認(rèn)版本的方法如下:
點(diǎn)擊右上的三個(gè)點(diǎn)->點(diǎn)擊瀏覽器的設(shè)置->點(diǎn)擊關(guān)于谷歌->查看版本(版本 112.0.5615.50(正式版本) (64 位))
查找對(duì)應(yīng)驅(qū)動(dòng)
http://chromedriver.storage.googleapis.com/index.html
查找和瀏覽器版本最近的一個(gè)驅(qū)動(dòng)版本(版本向下兼容)
博主的瀏覽器版本是:版本 112.0.5615.50(正式版本) (64 位)
所以下載的是112.0.5616.28 向下兼容,所以可以正常使用。
下載驅(qū)動(dòng)
下載chromedriver_win32.zip
系統(tǒng)是 64 位的,也可以使用 32 位的 ChromeDriver。因?yàn)?ChromeDriver
只是一個(gè)獨(dú)立的可執(zhí)行文件,它與您的操作系統(tǒng)架構(gòu)無(wú)關(guān)。只要您的 Chrome 瀏覽器和 ChromeDriver
版本匹配,就可以在任何系統(tǒng)上運(yùn)行 ChromeDriver。
解壓chromedriver.exe 存放到一個(gè)位置(后續(xù)會(huì)使用)
注意點(diǎn):
瀏覽器版本更新后,對(duì)應(yīng)的驅(qū)動(dòng)也需要更新才可以使用(可以去查找一下如何取消瀏覽器更新)
四·使用模塊
?? 我們以打開(kāi)百度瀏覽器為例:
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
service = Service(executable_path='D:\chorm\chromedriver_win32/chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.set_window_size(1100, 850) # 設(shè)置打開(kāi)的窗口大小
driver.get('https://www.baidu.com/?tn=02003390_84_hao_pg&') # 選擇自動(dòng)化操作頁(yè)面
input() # 加入input()是為了讓程序暫停,等待用戶輸入任意字符后才繼續(xù)執(zhí)行下一步操作。
運(yùn)行結(jié)果:
運(yùn)行成功后,他會(huì)幫我們自動(dòng)打開(kāi)百度網(wǎng)頁(yè)。我們可以看見(jiàn)上方出現(xiàn)chrome正受到自動(dòng)測(cè)試軟件的控制
加入input()是為了讓程序暫停,等待用戶輸入任意字符后才繼續(xù)執(zhí)行下一步操作。
這樣做是為了防止程序執(zhí)行完畢后自動(dòng)關(guān)閉瀏覽器窗口,讓用戶有足夠的時(shí)間觀察程序的執(zhí)行結(jié)果或手動(dòng)進(jìn)行后續(xù)操作。
selenium選取元素方法
-
find_element
獲取滿足條件的第一個(gè)元素 -
find_elements
獲取滿足條件的所有元素
序號(hào) | 方法 | 描述 |
---|---|---|
1 | find_element_by_id() | 通過(guò)ID定位元素 |
2 | find_element_by_name() | 通過(guò)name定位元素 |
3 | find_element_by_class_name() | 通過(guò)類樣式名稱定位元素 |
4 | find_element_by_tag_name() | 通過(guò)標(biāo)簽名稱定位元素 |
5 | find_element_by_link_text() | 通過(guò)鏈接定位元素(a標(biāo)簽) |
6 | find_element_by_css_selector() | 通過(guò)CSS定位元素 |
7 | find_element_by_xpath() | 通過(guò)xpath語(yǔ)法來(lái)獲取元素 |
示例:
from selenium.webdriver.common.by import By
driver.find_element(By.ID, 'kw').send_keys('飛機(jī)')
在百度輸入框自動(dòng)輸入夕陽(yáng)兩個(gè)字。
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
service = Service(executable_path='D:\chorm\chromedriver_win32/chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.set_window_size(1100, 850) # 設(shè)置打開(kāi)的窗口大小
driver.get('https://www.baidu.com/?tn=02003390_84_hao_pg&') # 選擇自動(dòng)化操作頁(yè)面
from selenium.webdriver.common.by import By
driver.find_element(By.ID, 'kw').send_keys('夕陽(yáng)')
input() # 加入input()是為了讓程序暫停,等待用戶輸入任意字符后才繼續(xù)執(zhí)行下一步操作。
接下來(lái)我們使用代碼來(lái)操作,讓它執(zhí)行搜索功能:
selenium嵌套頁(yè)面元素定位
import time
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
service = Service(executable_path='D:\chorm\chromedriver_win32/chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get('https://tieba.baidu.com/')
# 方法1 直接使用嵌套的網(wǎng)頁(yè)進(jìn)行使用
# 跳轉(zhuǎn)到嵌套網(wǎng)頁(yè)
# 選取嵌套網(wǎng)頁(yè)元素
element = driver.find_element(By.ID, 'iframeu6739266_0')
# 切換網(wǎng)頁(yè)
driver.switch_to.frame(element)
driver.find_element(By.ID, 'title0').click()
time.sleep(5)
selenium網(wǎng)頁(yè)下拉
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
import time
service = Service(executable_path='D:\chorm\chromedriver_win32/chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get('https://www.duitang.com/category/?cat=wallpaper')
s = 0
n = 100
for i in range(s, n, 5): # 實(shí)現(xiàn)網(wǎng)頁(yè)滑動(dòng)下拉
js = 'window.scrollTo(0,%s)' % (i * 100)
driver.execute_script(js)
time.sleep(0.5)
運(yùn)行這段程序后,會(huì)自動(dòng)打開(kāi)堆糖網(wǎng)頁(yè),并且實(shí)現(xiàn)自動(dòng)下拉頁(yè)面,方便我們?yōu)g覽。
selenium下拉表選擇
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
service = Service(executable_path='F:\chrom\chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get('https://kyfw.12306.cn/otn/regist/init')
from selenium.webdriver.support.ui import Select
select_element = driver.find_element(By.ID,'cardType')
select = Select(select_element)
selenium行為鏈
Selenium行為鏈(ActionChains)是Selenium中的一個(gè)Python庫(kù),它允許我們模擬用戶的交互行為,例如鼠標(biāo)移動(dòng)、單擊、雙擊、右鍵單擊等。使用行為鏈,我們可以創(chuàng)建一個(gè)動(dòng)作序列,然后將其執(zhí)行在我們的Web應(yīng)用程序上,從而模擬用戶的行為。
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
service = Service(executable_path='F:\chrom\chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get('https://www.baidu.com/')
action = ActionChains(driver) # 在driver創(chuàng)建行為鏈對(duì)象
inp = driver.find_element(By.ID, 'kw') # 獲取到輸入框位置
action.move_to_element(inp) # 把鼠標(biāo)移動(dòng)到輸入框
action.send_keys_to_element(inp, '百度貼吧') # 模擬輸入
btn = driver.find_element(By.ID, 'su') # 獲取搜索按鈕
action.move_to_element(btn) # 移動(dòng)鼠標(biāo)到搜索按鈕
action.click(btn) # 模擬點(diǎn)擊
action.perform() # 注意記得寫(xiě) 執(zhí)行行為
selenium等待
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located as PE
service = Service(executable_path='F:\chrom\chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get('https://movie.douban.com/typerank?type_name=%E5%89%A7%E6%83%85&type=11&interval_id=100:90&action=')
# 肖申克的救贖
# 需要等待頁(yè)面加載完成再執(zhí)行打印
# time.sleep(5)
# print(driver.page_source)
wait = WebDriverWait(driver, 10) # 等待10秒,有數(shù)據(jù)就進(jìn)行操作,沒(méi)有就報(bào)錯(cuò)
wait.until(PE((By.CLASS_NAME, 'rank-num'))).click()
print(driver.page_source)
五·錯(cuò)誤解決方案
( 1)109.0.5414.75
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 86
Current browser version is 109.0.5414.75 with binary path C:\ProgramFiles\Google\Chrome\Application\chrome.exe
需要指定驅(qū)動(dòng)位置
1.將驅(qū)動(dòng)放置在代碼的同級(jí)目錄
2. 直接指定驅(qū)動(dòng)的位置
(2) 瀏覽器閃退
selenium代碼沒(méi)有進(jìn)行阻塞 和瀏覽器的驅(qū)動(dòng)版本是有關(guān)系
如果想要阻塞 可以在代碼最后加上sleep(10) 或者是input()
(3)運(yùn)行代碼出現(xiàn)警告
F:\pythonSpider2208\day14-selenium\selenium自動(dòng)化.py:5: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path=r'F:\chrom\chromedriver.exe')
需要我們更新代碼,使用新的方式指定驅(qū)動(dòng)位置(代碼依然是可以正常使用的)
(4) 元素選擇異常
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="wd"]"}
(5)元素沒(méi)有選擇到
檢測(cè)value參數(shù)是否正確文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-412813.html
六·結(jié)束語(yǔ)
????本專欄所有文章是博主學(xué)習(xí)筆記,僅供學(xué)習(xí)使用,爬蟲(chóng)只是一種技術(shù),希望學(xué)習(xí)過(guò)的人能正確使用它。
博主也會(huì)定時(shí)一周三更爬蟲(chóng)相關(guān)技術(shù)更大家系統(tǒng)學(xué)習(xí),如有問(wèn)題,可以私信我,沒(méi)有回,那我可能在上課或者睡覺(jué),寫(xiě)作不易,感謝大家的支持?。??????文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-412813.html
到了這里,關(guān)于14.網(wǎng)絡(luò)爬蟲(chóng)—selenium詳講的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!