1. 绠�浠�
銆�銆�鏈枃鏄粌涔犵瘒鐨勬渶鍚庝竴绡囨枃绔狅紝铏界劧缁冧範绡囩殑鏂囩珷鍒版灏辫鍜屽ぇ瀹惰鎷滄嫓浜嗭紝浣嗘槸鎴戜滑鐨勫涔犱箣璺墠鍒氬垰寮�濮嬨�備笉瑕佸仠涓嬩綘鐨勮剼姝ワ紝澶ф鏈濆墠璧板惂锛佹瘮浣犱紭绉�鐨勪汉杩樺湪璧扮潃锛屾垜浠湁浠�涔堢悊鐢卞仠涓嬭嚜宸辩殑鑴氭浜嗭紝鐢熷懡涓嶆锛屽涔犱害鏄姝ゃ�傚ソ浜嗭紝瀹忓摜鐨勬瘨楦℃堡濂�
鍠濆惂锛屽枬澶熶簡灏卞紑濮嬪涔犲惂銆�
銆�銆�鏈枃涓昏鏄粌涔犲浣曞鐞唅frame鍒囨崲銆佸鐞哸lert寮圭獥銆佽幏鍙栧綋鍓嶉〉闈㈠叏閮ㄤ俊鎭�佽幏鍙栭〉闈ref灞炴�у拰濡備綍鎴浘淇濆瓨绛夌瓑鍐呭锛屽笇鏈涘皬浼欎即鎴栬�呯闉嬩滑鍠滄鍝堬紒锛侊紒
2. 澶勭悊iframe鍒囨崲
銆�銆�鏈枃浠嬬粛濡備綍鍒╃敤Selenium涓柟娉曪紝鍦ㄤ笉鍚宨frame涓垏鎹€��
闂锛氭湁鏃跺�欐垜浠彂鐜板厓绱犲畾浣嶆病闂锛屽湪娴嬭瘯鍥炴斁鐨勮繃绋嬶紝鍙戠幇灏辨槸鎵句笉鍒板厓绱犳姤閿欍��
2.1 绀轰緥涓�锛�
銆�銆�鐢变簬娌℃湁鎵惧埌鍚堥�傜殑iframe缃戠珯锛岃繖閲屼笉濂界敤浠g爜涓句緥锛岀畝鍗曟枃瀛楀姞鍥剧墖鏉ヤ粙缁嶃�備絾鏄畯鍝ヨ緱杞弽渚х殑鐫′笉鐫�锛屾渶鍚庡疄鍦ㄦ槸娌℃嫑浜嗐�備簬鏄嚜宸卞啓浜嗕竴涓畝闄嬫瘮杈僱ow鐨勭綉椤碉紝鍒╃敤iframe缃戦〉妗嗘灦锛屾妸缃戦〉鍒嗘垚涓婁笅涓や釜涓嶅悓iframe鎺у埗鐨勯〉闈紝鎶婁笅闈tml浠g爜淇濆瓨鍒�
涓�涓浜嬫湰锛岀劧鍚庝慨鏀瑰悕绉颁负index.html銆�
2.2 index.html
<html> <head><title>iframe test pagetitle>head> <body> <iframe id="frame1" src="https://www.baidu.com" name="slider" height="325" width="550">iframe> <br> <iframe id="frame2" src="http://example.com" name="example" height="325" width="550">iframe> body> html>
鐢ㄦ湰鍦版祻瑙堝櫒鎵撳紑濡備笅鍥撅細
鐢╢irepath鏉ヨ幏寰楃櫨搴︽枃鏈緭鍏ユ鐨刋Path鎴浘
鐪嬪埌浜嗙孩鍦堜綅缃笉鏄樉绀篢op Window锛岃鏄庝綘瑕佸畾浣嶇殑鐩爣鍏冪礌鍦ㄤ竴涓猧frame閲岋紝濡傛灉瑕佹搷浣滆繖涓厓绱狅紝棣栧厛闇�瑕佷粠榛樿鐨凾op Window锛屽埄鐢╯witch_to.frame('iframeid')鏂规硶鏉ュ垏鎹㈠埌鍏蜂綋鐨刬frame锛岀劧鍚庢墠鑳藉幓鎿嶄綔鐩爣鍏冪礌銆�
姝e父鐧惧害椤甸潰锛屾枃鏈緭鍏ユ鎴浘
鏂规硶浠嬬粛锛�
driver.switch_to.frame("iframe1")
# 鎿嶄綔鐩爣鍏冪礌锛岃繖涓洰鏍囧厓绱犲湪 iframe1閲岄潰锛岃繖閲屽氨鏄櫨搴︽枃鏈緭鍏ユ杈撳叆鏂囧瓧
driver.switch_to.default_content()
濡傛灉鏈夊叿浣撻」鐩〉闈㈠埄鐢ㄤ簡iframe, 鑷繁鍔ㄦ墜鍐欎竴涓嬭剼鏈��
2.3 绀轰緥浜岋細
銆�銆�瀹忓摜鏈�鍚庣粓浜庢壘鍒颁簡涓�涓悎閫傜殑demo鏉ョ粰灏忎紮浼翠滑鎴栬�呯闉嬩滑鏉ョ敤浠g爜瀹為檯婕旂粌鐨勪緥瀛愬涓嬶細
姝ラ锛�
1.鍏堢‘璁や綘瑕佹搷浣滅殑鍏冪礌锛屾槸鍚﹀瓨鍦ㄤ笌iframe涓紝濡傛灉鍏冪礌鍦╥frame涓氨闇�瑕佸垏鎹�
2.鎵惧埌璇frame
3.鍒囨崲鍒拌iframe 锛堜袱绉嶆柟寮忥級
绗竴绉嶆柟寮忥細鏈変笁绉嶆柟娉曪紝鍙槸鍗曠函鐨勫垏鎹�
driver.switch_to.frame(1) #閫氳繃index锛堜笅鏍囷級
driver.switch_to.frame('login_frame_qq') #閫氳繃name
driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@name="login_frame_qq"]')) #閫氳繃webelement
绗袱绉嶆柟寮忥細鏃㈢瓑寰呭厓绱犲彲瑙佸張杩涜浜唅frame鍒囨崲
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it('login_frame_qq'))
4.鍒囨崲瀹屽悗锛屽氨浠ヨ繖涓猧frame涓殑html涓轰富html
5.杩斿洖榛樿鐨刪tml椤甸潰锛屾棤璁虹幇鍦ㄥ湪閭d竴灞傜殑iframe锛屾墽琛屼竴娆″嵆鍙��
driver.switch_to.default_content()
2.3.1 浠g爜瀹炵幇锛�
2.3.2 鍙傝�冧唬鐮侊細
# coding=utf-8馃敟 # 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛� # 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆� ''' Created on 2019-12-09 @author: 鍖椾含-瀹忓摜 QQ浜ゆ祦缇わ細705269076 Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒8 ''' # 3.瀵煎叆妯″潡 import time from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.maximize_window() # 鐧诲綍鑵捐璇惧爞椤甸潰 driver.get('https://ke.qq.com/course/list') # 鐐瑰嚮棣栭〉鐧诲綍 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//a[@id="js_login"]'))) driver.find_element_by_id("js_login").click() # 鐧诲綍寮圭獥鐐瑰嚮QQ鐧诲綍 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//a[contains(@class,"btns-enter-qq")]'))) driver.find_element_by_xpath('//a[contains(@class,"btns-enter-qq")]').click() # iframe鍒囨崲 # driver.switch_to.frame('login_frame_qq') #閫氳繃name driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@name="login_frame_qq"]')) #閫氳繃webelement # 寮圭獥鐐瑰嚮璐︽埛瀵嗙爜鐧诲綍switcher_plogin #WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//a[@id="switcher_plogin"]'))) time.sleep(10) driver.find_element_by_xpath('//a[@id="switcher_plogin"]').click()
2.3.3 杩愯缁撴灉锛�
杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
2.3.4 娴忚鍣ㄨ繍琛岀粨鏋滐細
娴忚鍣ㄧ殑杩愯杩囩▼锛屽畯鍝ヨ繖閲屽氨涓嶅仛灞忓箷褰曞埗浜嗭紝杩欎釜鎴戜滑鐪嬩竴涓嬬粨鏋溿�傛祻瑙堝櫒杩愯缁撴灉濡備笅锛�
3. 澶勭悊Alert寮圭獥
鏈枃鏉ヤ粙缁嶅浣曢�氳繃Selenium鏂规硶鍘诲鐞嗙綉椤礎lert寮圭獥锛屽拰澶勭悊iframe绫讳技锛岄兘鏄�氳繃switch_to鏂规硶銆傝繖閲岃繕鏄病鏈夋壘鍒板悎閫傜殑alert寮圭獥缃戠珯锛屾垜浠氨鑷繁鍒涘缓涓�涓惂锛屽墠闈㈡枃绔犱粙缁嶄簡濡備綍閫氳繃鎵цJS鏉ュ鍔犱竴涓脊绐椼��
锛�1锛変娇鐢╯witch_to鏂规硶鍏堝垏鎹㈠埌娴忚鍣ㄥ脊鍑烘
driver.switch_to.alert
锛�2锛堿lert绫绘彁渚涗簡涓�绯诲垪鐨勬搷浣滄柟娉�
dismiss(): 鍚�
accept(): 鏄�
text(): 鑾峰彇寮瑰嚭妗嗛噷鐨勬枃鏈唴瀹�
send_keys(): 鍦ㄥ脊鍑烘閲岃緭鍏ユ枃鏈�
鐩稿叧鑴氭湰浠g爜濡備笅锛�
3.1 浠g爜瀹炵幇锛�
3.2 鍙傝�冧唬鐮侊細
# coding=utf-8馃敟 # 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛� # 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆� ''' Created on 2019-12-09 @author: 鍖椾含-瀹忓摜 QQ浜ゆ祦缇わ細705269076 Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒8 ''' # 3.瀵煎叆妯″潡 import time from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(6) driver.get("https://www.baidu.com") time.sleep(1) driver.execute_script("window.alert('杩欐槸涓�涓祴璇旳lert寮圭獥');") time.sleep(2) driver.switch_to_alert().accept() # 鐐瑰嚮寮瑰嚭閲岄潰鐨勭‘瀹氭寜閽� #driver.switch_to_alert().dismiss() # 鐐瑰嚮寮瑰嚭涓婇潰鐨刋鎸�
3.3 杩愯缁撴灉锛�
杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
娉ㄦ剰锛�
driver.switch_to_alert().accept()
杩欐槸涓�涓�佹柟娉曪紝鍦ㄦ湁浜涚紪杈戝櫒锛堝悗闈細浠嬬粛涓�娆綪ython鐨処DE宸ュ叿锛変細鎻愮ず杩欎釜鏂规硶鍒掓í绾匡紝璇存槑鍦ㄦ渶鏂癝elenium鍦≒yhton鏀寔鍖呴噷锛岃繖涓柟娉曡涓㈠純锛岃櫧鐒惰繕鏄彲浠ョ敤锛岀幇鍦ㄦ柊鐨勬柟娉曟槸switch_to.alert(),鐢ㄨ繖涓渶鏂版柟娉曪紝鎴戞祴璇曚簡涓嬶紝鏃犳硶妯℃嫙鐐瑰嚮纭畾鍜岀偣鍑诲叧
闂脊绐楁寜閽紝鎵�浠ヨ繖涓湴鏂归�夋嫨浜嗘棫鐨勬柟娉晄witch_to_alert()
4. 鑾峰彇褰撳墠椤甸潰鍏ㄩ儴鍥剧墖淇℃伅
鏈枃鏉ヤ粙缁嶄笅濡備綍鑾峰彇褰撳墠椤甸潰鍏ㄩ儴鐨勫浘鐗囦俊鎭紝鍥剧墖淇℃伅鍙兘鍖呮嫭锛屽浘鐗囧悕绉帮紝鍥剧墖澶у皬绛夈��
鐩稿叧鑴氭湰浠g爜濡備笅锛�
4.1 浠g爜瀹炵幇锛�
4.2 鍙傝�冧唬鐮侊細
# coding=utf-8馃敟 # 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛� # 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆� ''' Created on 2019-12-09 @author: 鍖椾含-瀹忓摜 QQ浜ゆ祦缇わ細705269076 Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒8 ''' # 3.瀵煎叆妯″潡 import time from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(6) driver.get("http://news.baidu.com") time.sleep(1) for image in driver.find_elements_by_tag_name("img"): print (image.text) print (image.size) print (image.tag_name)
4.3 杩愯缁撴灉锛�
杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
杩愯缁撴灉锛�
杩愯缁撴灉锛屽彂鐜版病鏈夊浘鐗囧悕绉版墦鍗板嚭鏉ワ紝璇存槑鐧惧害鏂伴椈椤甸潰锛屾墍鏈夊浘鐗囬兘娌℃湁缁欏嚭text杩欎釜灞炴�э紝鍓嶇鐨勫瀛愭病鏈夊啓鍥剧墖鐨則ext灞炴�с�備笉鐩镐俊鐨勫悓瀛︽垨鑰呭皬浼欎即浠彲浠ラ�氳繃F12鏌ョ湅涓�涓嬪浘鐗囨槸鍚︽湁text杩欎釜灞炴�с��
5. 鑾峰彇椤甸潰鍏冪礌鐨刪ref灞炴��
鏈枃鏉ヤ粙缁嶅浣曢�氳繃Selenium鑾峰彇椤甸潰鍏冪礌鐨勬煇涓�涓睘鎬с�備竴涓厓绱犲彲鑳芥湁澶氫釜灞炴�э紝渚嬪 class, id, name, text, href, vale绛夌瓑銆傝繖閲屾垜浠妇渚嬩竴涓埇铏腑缁忓父闇�瑕佸鐞嗙殑閾炬帴闂锛氭壘鍑哄綋鍓嶉〉闈㈡墍鏈夌殑瓒呴摼鎺ャ��
宸茬櫨搴﹂椤典负渚嬶紝鎵撳嵃鎵�鏈夊寘鍚玥ref鐨勫厓绱犵殑閾炬帴銆�
鐩稿叧鑴氭湰浠g爜濡備笅锛�
5.1 浠g爜瀹炵幇锛�
5.2 鍙傝�冧唬鐮侊細
# coding=utf-8馃敟 # 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛� # 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆� ''' Created on 2019-12-09 @author: 鍖椾含-瀹忓摜 QQ浜ゆ祦缇わ細705269076 Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒8 ''' # 3.瀵煎叆妯″潡 import time from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(6) driver.get("https://www.baidu.com") time.sleep(1) for link in driver.find_elements_by_xpath("//*[@href]"): print (link.get_attribute('href')) driver.quit()
5.3 杩愯缁撴灉锛�
杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
杩欓噷鍙槸鎵惧厓绱犵殑href灞炴�э紝濡傛灉浣犻渶瑕佸叾瀹冨睘鎬э紝渚嬪浣犻渶瑕佹煡鐪嬮〉闈㈡墍鏈夊厓绱犲叿鏈塱d鍊肩殑璇濓紝浣犲彲浠ヨ繖鏍峰啓锛� print (link.get_attribute('id'))
6. 濡備綍鎴浘骞朵繚瀛�
鏈枃浠嬬粛濡備綍鍒╃敤Selenium鐨勬柟娉曡繘琛屾埅鍥撅紝鍦ㄦ祴璇曡繃绋嬩腑锛屾槸鏈夊繀瑕佹埅鍥撅紝鐗瑰埆鏄亣鍒伴敊璇殑鏃跺�欒繘琛屾埅鍥俱�傚湪selenium for python涓富瑕佹湁涓変釜鎴浘鏂规硶锛屾垜浠寫閫夊叾涓渶甯哥敤鐨勪竴绉嶃��
get_screenshot_as_file()
鐩稿叧浠g爜濡備笅锛�
6.1 浠g爜瀹炵幇锛�
6.2 鍙傝�冧唬鐮侊細
# coding=utf-8馃敟 # 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛� # 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆� ''' Created on 2019-12-09 @author: 鍖椾含-瀹忓摜 QQ浜ゆ祦缇わ細705269076 Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒8 ''' # 3.瀵煎叆妯″潡 import time from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(6) driver.get("https://www.baidu.com") time.sleep(1) driver.get_screenshot_as_file("C:\\Users\\浣犵殑璐︽埛鍚峔\Desktop\\baidu.png") driver.quit()
6.3 杩愯缁撴灉锛�
杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
6.4 鎵ц缁撴灉锛�
浼氬湪妗岄潰淇濆瓨涓�寮犵櫨搴﹂椤电殑鎴浘锛屽浘鐗囧悗缂�鏄痯ng銆傛敞鎰忚矾寰勬槸瑕佷袱涓� \\ 銆傚涓嬪浘锛�
7. 灏忕粨
7.1 澶氫釜iframe鐨勫垏鎹㈤棶棰�
# 濡傦細鏈変袱涓猧frame锛歠1銆乫2 # 1.瀹氫綅鍒癴1 driver.switch_to_frame("f1") # 2.鎿嶄綔鍏冪礌 # ······ # 3.閫�鍑篿frame driver.switch_to_default_content() # 4.瀹氫綅鍒癴2 driver.switch_to_frame("f2") # ······
7.2 宓屽锛歠1涓祵濂楃潃f2
driver.switch_to_frame("f1") driver.switch_to_frame("f2") # 鎿嶄綔鍏冪礌
7.3 閫�鍑篿frame锛�
# 绗竴绉嶆柟寮忥細璺冲嚭鎵�鏈塱frame锛屽洖鍒颁富鐣岄潰 driver.switch_to_default_content() # 绗簩绉嶆柟寮忥細鍥炲埌f1锛堣繑鍥炰笂涓�绾э級 driver.switch_to.parent_frame()
濂戒簡锛屼粖澶╃殑缁冧範灏卞埌杩欓噷锛屽笇鏈涘ぇ瀹跺ソ濂界殑缁冧範鍜岀悊瑙c��
鎮ㄧ殑鑲畾灏辨槸鎴戣繘姝ョ殑鍔ㄥ姏銆�濡傛灉浣犳劅瑙夎繕涓嶉敊锛屽氨璇烽紦鍔变竴涓嬪惂锛佽寰楃偣娉� 鎺ㄨ崘 涓嶈蹇樿鍝︼紒锛侊紒