日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python Chrome + selenium自动化测试与python爬虫获取网页数据

發(fā)布時(shí)間:2025/3/11 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python Chrome + selenium自动化测试与python爬虫获取网页数据 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、使用Python+selenium+Chrome 報(bào)錯(cuò):
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 78

說明,這個(gè)chrom驅(qū)動(dòng)支持78版本

谷歌瀏覽器版本


進(jìn)入驅(qū)動(dòng)網(wǎng)址:http://npm.taobao.org/mirrors/chromedriver/,下載對(duì)應(yīng)版本的谷歌驅(qū)動(dòng)

下載后放在與python解析器同一文件夾下面

重新運(yùn)行程序,運(yùn)行成功!!!

二、實(shí)例測試:
使用python selenium自動(dòng)化測試模塊結(jié)合python爬蟲獲取網(wǎng)頁數(shù)據(jù)。

使用谷歌瀏覽器模擬打開https://www.hao123.com/網(wǎng)頁,瀏覽器點(diǎn)擊百度銜接,模擬輸入python進(jìn)行百度搜索,在關(guān)閉子網(wǎng)頁,最后在https://www.hao123.com/網(wǎng)頁獲取精選新聞信息。


1.python控制谷歌瀏覽器
main.py

"""=== coding: UTF8 ===""" from selenium.webdriver import Chrome from selenium.webdriver.common.keys import Keys import time""" ======================================== 主函數(shù)功能測試 ======================================== """ if __name__ == '__main__':web = Chrome()web.get("https://www.hao123.com/")# 谷歌瀏覽器模擬人工點(diǎn)擊“百度”銜接web.find_element_by_xpath('//*[@id="userCommonSites"]/ul/li[1]/div/a').click()time.sleep(1)# 變更selenium窗口視角,切換到子窗口,-1代表在網(wǎng)頁標(biāo)簽頁最后一個(gè)網(wǎng)頁web.switch_to.window(web.window_handles[-1])# 谷歌瀏覽器模擬人工輸入“python”,進(jìn)行百度搜索web.find_element_by_xpath('//*[@id="kw"]').send_keys("python", Keys.ENTER)time.sleep(1)# 關(guān)閉子窗口web.close()# 變更selenium窗口視角,回到原來的窗口web.switch_to.window(web.window_handles[-1])# 提取精選新聞內(nèi)容a_list = web.find_elements_by_xpath('//*[@id="topzixun-over"]/div/div[2]/p')for a in a_list:print(a.find_element_by_xpath('./a[1]').text)# 關(guān)閉窗口web.close()# 對(duì)于嵌入的視頻窗口,切換到iframe窗口# iframe = web.find_elements_by_xpath('......')# web.switch_to.frame(iframe)# 切換到原畫面# web.switch_to.default_content()

2.python使谷歌瀏覽器在后臺(tái)運(yùn)行,爬取數(shù)據(jù)(即無頭瀏覽器)
main.py

"""=== coding: UTF8 ===""" from selenium.webdriver import Chrome from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options import time""" ======================================== 主函數(shù)功能測試 ======================================== """ if __name__ == '__main__':# 準(zhǔn)備好參數(shù)配置(使谷歌瀏覽器在后臺(tái)運(yùn)行,即無頭瀏覽器)opt = Options()opt.add_argument("--headless")opt.add_argument("--disbale-gpu")# 把參數(shù)配置到谷歌瀏覽器中web = Chrome(options=opt)web.get("https://www.hao123.com/")# 谷歌瀏覽器模擬人工點(diǎn)擊“百度”銜接web.find_element_by_xpath('//*[@id="userCommonSites"]/ul/li[1]/div/a').click()time.sleep(1)# 變更selenium窗口視角,切換到子窗口,-1代表在網(wǎng)頁標(biāo)簽頁最后一個(gè)網(wǎng)頁web.switch_to.window(web.window_handles[-1])# 谷歌瀏覽器模擬人工輸入“python”,進(jìn)行百度搜索web.find_element_by_xpath('//*[@id="kw"]').send_keys("python", Keys.ENTER)time.sleep(1)# 關(guān)閉子窗口web.close()# 變更selenium窗口視角,回到原來的窗口web.switch_to.window(web.window_handles[-1])# 提取精選新聞內(nèi)容a_list = web.find_elements_by_xpath('//*[@id="topzixun-over"]/div/div[2]/p')for a in a_list:print(a.find_element_by_xpath('./a[1]').text)# 關(guān)閉窗口web.close()# 對(duì)于嵌入的視頻窗口,切換到iframe窗口# iframe = web.find_elements_by_xpath('......')# web.switch_to.frame(iframe)# 切換到原畫面# web.switch_to.default_content()

關(guān)注公眾號(hào),獲取更多資料

總結(jié)

以上是生活随笔為你收集整理的python Chrome + selenium自动化测试与python爬虫获取网页数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。