涓銆丼elenium搴撳埌搴曟槸浠涔堬紵
Selenium 鏄竴涓己澶х殑鑷姩鍖栨祴璇曞伐鍏凤紝瀹冨彲浠ヨ浣犵洿鎺ユ搷鎺ф祻瑙堝櫒锛屽畬鎴愬悇绉嶄笌缃戦〉浜や簰鐨勪换鍔°傞氳繃浣跨敤 Python 鐨 Selenium 搴擄紝浣犲彲浠ラ珮鏁堝湴瀹炵幇缃戦〉鑷姩鍖栨祴璇曪紝浠庤岃妭鐪佸ぇ閲忔椂闂村拰绮惧姏銆

1.1 Selenium搴撶殑涓昏鍔熻兘
鑷姩鍖栨祴璇曪細鍙互瀹炵幇瀵圭綉绔欑殑鑷姩鍖栧姛鑳芥祴璇曪紝姣斿鐐瑰嚮鎸夐挳銆佽緭鍏ユ枃鏈侀夋嫨涓嬫媺鑿滃崟绛夈

缃戦〉鐖櫕锛氬浜庝竴浜涢渶瑕佺櫥褰曟垨鑰呮湁鍙嶇埇鏈哄埗鐨勭綉绔欙紝浣跨敤 Selenium 鍙互杞绘澗搴斿銆

鑷姩鍖栦换鍔★細濡傚畾鏃惰闂綉绔欍佽嚜鍔ㄦ彁浜よ〃鍗曠瓑銆

1.2 Selenium搴撶殑浼樺娍
鏀寔澶氱缂栫▼璇█锛歅ython銆丣ava銆丆#銆丷uby 绛夈

鏀寔澶氱娴忚鍣細Chrome銆丗irefox銆丒dge銆丼afari 绛夈

璺ㄥ钩鍙帮細Windows銆乵acOS銆丩inux銆

绀惧尯娲昏穬锛屾寔缁洿鏂扮淮鎶ゃ

浜屻丼elenium搴撶殑瀹夎涓庨厤缃
2.1 瀹夎Selenium搴
鍦ㄥ畨瑁 Selenium 搴撲箣鍓嶏紝璇风‘淇濆凡缁忓畨瑁呬簡 Python銆傛帴涓嬫潵锛屽彧闇鎵撳紑缁堢鎴栧懡浠ゆ彁绀虹锛岃緭鍏ヤ互涓嬪懡浠ゅ嵆鍙畨瑁 Selenium 搴擄細

pip install selenium
2.2 閰嶇疆娴忚鍣ㄩ┍鍔
瑕佷娇鐢 Selenium 鎺у埗娴忚鍣紝浣犺繕闇瑕佸畨瑁呭搴旀祻瑙堝櫒鐨勯┍鍔ㄣ傝繖閲屼互 Chrome 娴忚鍣ㄤ负渚嬶紝鍏朵粬娴忚鍣ㄩ┍鍔ㄧ殑瀹夎鏂规硶绫讳技銆

涓嬭浇 Chrome 椹卞姩锛氭悳 ChromeDriver 鍦ㄤ笅杞介〉闈㈤夋嫨涓庝綘鐨 Chrome 娴忚鍣ㄧ増鏈尮閰嶇殑椹卞姩銆

瑙e帇涓嬭浇鐨勫帇缂╁寘锛屽皢瑙e帇鍚庣殑 chromedriver 鏂囦欢鏀剧疆鍦ㄤ竴涓彲鎵ц璺緞涓嬶紝濡 /usr/local/bin锛坢acOS銆丩inux锛夋垨 C:\Windows锛圵indows锛夈

涓夈佷娇鐢⊿elenium搴撹繘琛屽熀鏈搷浣
3.1 鍚姩娴忚鍣
瀵煎叆 Selenium 鐨 webdriver 妯″潡锛岀劧鍚庡疄渚嬪寲涓涓 Chrome 椹卞姩瀵硅薄锛屽氨鍙互鍚姩 Chrome 娴忚鍣ㄤ簡锛

from selenium import webdriver
 
driver = webdriver.Chrome()
3.2 鎵撳紑缃戦〉
浣跨敤 get 鏂规硶锛屼紶鍏ョ洰鏍囩綉鍧锛屽嵆鍙娴忚鍣ㄦ墦寮璇ョ綉鍧锛

driver.get("https://www.baidu.com")
3.3 瀹氫綅椤甸潰鍏冪礌
Selenium 鎻愪緵浜嗗绉嶅畾浣嶉〉闈㈠厓绱犵殑鏂规硶锛屽 find_element_by_id銆乫ind_element_by_name銆乫ind_element_by_class_name 绛夈傝繖閲屼互鐧惧害鎼滅储妗嗕负渚嬶紝浣跨敤 find_element_by_id 鏂规硶瀹氫綅锛

search_box = driver.find_element_by_id("kw")
3.4 鎿嶄綔椤甸潰鍏冪礌
鎵惧埌椤甸潰鍏冪礌鍚庯紝浣犲彲浠ュ鍏惰繘琛屽悇绉嶆搷浣滐紝濡傝緭鍏ユ枃鏈佺偣鍑绘寜閽瓑銆傝繖閲屼互杈撳叆鎼滅储鍏抽敭璇嶄负渚嬶細

search_box.send_keys("Selenium")
3.5 鎻愪氦琛ㄥ崟
鍦ㄦ悳绱㈡杈撳叆鍏抽敭璇嶅悗锛岃繕闇瑕佺偣鍑烩滅櫨搴︿竴涓嬧濇寜閽潵鎻愪氦琛ㄥ崟銆傝繖閲岀敤 find_element_by_id 鏂规硶瀹氫綅鎸夐挳锛岀劧鍚庣敤 click 鏂规硶杩涜鐐瑰嚮锛

search_button = driver.find_element_by_id("su")
search_button.click()
3.6 绛夊緟椤甸潰鍔犺浇
鍦ㄨ繘琛岃嚜鍔ㄥ寲娴嬭瘯鏃讹紝寰堝鏃跺欓渶瑕佺瓑寰呴〉闈㈠姞杞姐係elenium 鎻愪緵浜嗕袱绉嶇瓑寰呮柟娉曪細鏄惧紡绛夊緟鍜岄殣寮忕瓑寰呫

3.6.1 鏄惧紡绛夊緟

鏄惧紡绛夊緟浼氬湪鎸囧畾鏃堕棿鍐咃紝涓嶆柇灏濊瘯鏌ユ壘鐩爣鍏冪礌锛岀洿鍒版壘鍒颁负姝€傝繖閲屼互绛夊緟鎼滅储缁撴灉鍑虹幇涓轰緥锛

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
 
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "content_left"))
    )
except TimeoutException:
    print("绛夊緟瓒呮椂锛")
3.6.2 闅愬紡绛夊緟

闅愬紡绛夊緟浼氬湪鏌ユ壘鍏冪礌鏃讹紝绛夊緟鎸囧畾鏃堕棿鍚庡啀鎶涘嚭寮傚父銆傝繖閲屼互绛夊緟鎼滅储缁撴灉鍑虹幇涓轰緥锛

driver.implicitly_wait(10)
content_left = driver.find_element_by_id("content_left")
3.7 鑾峰彇椤甸潰淇℃伅
鑾峰彇椤甸潰鐨勬爣棰樸乁RL銆佹簮浠g爜绛変俊鎭細

title = driver.title
url = driver.current_url
source = driver.page_source
 
print("鏍囬锛", title)
print("URL锛", url)
3.8 鍏抽棴娴忚鍣
瀹屾垚鎵鏈夋搷浣滃悗锛屽埆蹇樹簡鍏抽棴娴忚鍣細

driver.quit()
鍥涖丼elenium搴撶殑楂樼骇搴旂敤
4.1 鍒囨崲绐楀彛
鍦ㄨ繘琛岃嚜鍔ㄥ寲娴嬭瘯鏃讹紝鏈夋椂闇瑕佸垏鎹㈠埌鏂版墦寮鐨勭獥鍙c傝繖閲屼互鐐瑰嚮鐧惧害棣栭〉搴曢儴鐨 鈥滃叧浜庣櫨搴︹ 閾炬帴涓轰緥锛屾紨绀哄浣曞垏鎹㈢獥鍙o細

from selenium.webdriver.common.keys import Keys
 
about_link = driver.find_element_by_link_text("鍏充簬鐧惧害")
about_link.send_keys(Keys.CONTROL + Keys.RETURN)
 
driver.switch_to.window(driver.window_handles[-1])
4.2 鍒囨崲 Frame
鏈変簺缃戦〉浼氫娇鐢 Frame 宓屽椤甸潰锛岃繖鏃堕渶瑕佸垏鎹㈠埌瀵瑰簲鐨 Frame 鎵嶈兘鎿嶄綔鍏朵腑鐨勫厓绱犮傚垏鎹 Frame 鍙互鐢 switch_to.frame 鏂规硶锛

driver.switch_to.frame("frame_name")
鍒囨崲鍥炰富椤甸潰鍙互鐢 switch_to.default_content 鏂规硶锛

driver.switch_to.default_content()
4.3 鎵ц JavaScript 浠g爜
鏈夋椂鍊欙紝浣犲彲鑳介渶瑕佹墽琛屼竴浜 JavaScript 浠g爜鏉ュ疄鐜扮壒瀹氬姛鑳斤紝姣斿婊氬姩椤甸潰銆佷慨鏀瑰厓绱犲睘鎬х瓑銆傝繖閲屼互婊氬姩椤甸潰涓轰緥锛
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")




浠ヤ笂灏辨槸浠涔堟槸python Selenium搴撶殑浣滅敤鏄粈涔堬紵鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=287