環(huán)境:Windows
版本:python3,selenium 4.11.2
寫(xiě)這個(gè)是方便自己重裝電腦時(shí)重新裝 Selenium,懶得每次都重新找鏈接。
1 裝
Chrome 和 Edge 或其他瀏覽器任選其一。
Chrome
首先,終端運(yùn)行:
pip3 install selenium==4.11.2
官網(wǎng)下載Chrome:https://www.google.cn/intl/zh-CN/chrome/
安裝好Chrome之后查看Chrome版本:chrome://settings/help
如果Chrome版本大于114,官網(wǎng)下載與Chrome版本對(duì)于的ChromeDriver:https://googlechromelabs.github.io/chrome-for-testing/,往下翻翻就能看到下載鏈接;
如果Chrome版本小于等于114,官網(wǎng)下載ChromeDriver鏈接:https://chromedriver.chromium.org/downloads。
解壓下好的ChromeDriver.zip,把里面的exe拖出來(lái),并記住放到了哪個(gè)路徑。
寫(xiě)代碼引入driver:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s = Service("D:/software/chromedriver.exe")
driver = webdriver.Chrome(service=s)
結(jié)合Options使用的方式:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(
"user-agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'") # UA
s = Service("D:/software/chromedriver.exe")
driver = webdriver.Chrome(service=s, options=options)
Edge
首先,終端運(yùn)行:
pip3 install selenium==4.11.2
官網(wǎng)下載Edge:https://www.microsoft.com/en-us/edge/download
安裝好Edge之后查看Chrome版本:edge://settings/help
官網(wǎng)下載與Edge版本對(duì)于的webDriver:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
解壓下好的edgeDriver.zip,把里面的exe拖出來(lái),并記住放到了哪個(gè)路徑。
from selenium import webdriver
from selenium.webdriver.edge.service import Service
s = Service('D:/software/msedgedriver.exe')
edge = webdriver.Edge(service=s)
結(jié)合Options使用的方式:
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
options = Options()
options.add_argument(
"user-agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'") # UA
s = Service('D:/software/msedgedriver.exe')
edge = webdriver.Edge(service=s, options=options)
其他瀏覽器
瀏覽器本身直接搜索下載,驅(qū)動(dòng)driver可參考selenium官網(wǎng)的驅(qū)動(dòng)下載列表:
https://www.selenium.dev/zh-cn/documentation/webdriver/troubleshooting/errors/driver_location/#download-the-driver。
2 運(yùn)行報(bào)錯(cuò)
以下是運(yùn)行Selenium可能遇到的問(wèn)題:
RequestsDependencyWarning: urllib3 (1.26.9) or chardet (3.0.4) doesn‘t match a supported version
解決:更新requests:pip3 install --upgrade requests
打開(kāi)了瀏覽器,但是沒(méi)有顯示網(wǎng)頁(yè) / Service連接失敗
原因:瀏覽器驅(qū)動(dòng)版本下載錯(cuò)誤。
解決:請(qǐng)自行確定瀏覽器版本并重新下載驅(qū)動(dòng)driver。
invalid argument: invalid locator (Session info: MicrosoftEdge=102.0.1245.44)
兩種原因:
① 瀏覽器版本和驅(qū)動(dòng)版本不一致(請(qǐng)自行確定瀏覽器版本并重新下載驅(qū)動(dòng)driver);
② 代碼打錯(cuò)了。比如edge.find_elements(by="http://div")
,正確的是edge.find_elements(by='xpath',value="http://div")
。
3 老代碼報(bào)錯(cuò)
Selenium 4重構(gòu)過(guò),API 發(fā)生了一些變化。以下是常見(jiàn)報(bào)錯(cuò):
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
原因:查詢(xún)當(dāng)前版本重構(gòu)后的函數(shù),是之前的 executable_path 被重構(gòu)到了 Service 函數(shù)里。所以,新版的selenium不能繼續(xù)用executable_path,而是應(yīng)該寫(xiě)成Service。
DeprecationWarning 警告的類(lèi)型錯(cuò)誤的意思都是,該類(lèi)型的警告大多屬于版本已經(jīng)更新,所使用的方法過(guò)時(shí)。
解決:webdriver.Edge(executable_path='/pathto/webdriver.exe', options=options)
改成 webdriver.Edge(service=Service('/pathto/webdriver.exe'), options=options)
,意思是去掉代碼中的executable_path,用Service,如本文的第一節(jié)裝
里提供的示例代碼那樣寫(xiě)。
參考:selenium 報(bào)錯(cuò) DeprecationWarning: executable_path has been deprecated, please pass in a Service object
AttributeError: ‘WebDriver’ object has no attribute ‘find_elements_by_xpath’
原因:新的driver類(lèi)沒(méi)有find_elements_by_xpath
方法了:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-728180.html
解決:改成find_elements(by='xpath', value='查找路徑')
。
快速替換:find_elements_by_xpath(
→ find_elements(by='xpath',value=
。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-728180.html
4 經(jīng)典代碼片段分享
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
def connectchrome():
"""
連接chrome瀏覽器,實(shí)現(xiàn)無(wú)痕瀏覽
"""
# driver_path = os.getcwd()+ "/chromedriver.exe"
# s = Service(driver_path)
s = Service("D:/software/chromedriver.exe") # 注意改成自己的driver路徑
options = Options()
options.add_argument('log-level=3')
options.add_argument("--incognito")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option('excludeSwitches', ['enable-automation'])
prefs = {
'profile.default_content_setting_values': {
'images': 2,
}
}
options.add_experimental_option('prefs', prefs)
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument(
"user-agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'")
driver = webdriver.Chrome(service=s, options=options)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
driver.set_window_size(1280, 800)
driver.set_window_position(100, 100)
time.sleep(2)
return driver
到了這里,關(guān)于【記錄】Python3|Selenium4 極速上手入門(mén)(Windows)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!