銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�

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>

鐢ㄦ湰鍦版祻瑙堝櫒鎵撳紑濡備笅鍥撅細

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第1张图片

鐢╢irepath鏉ヨ幏寰楃櫨搴︽枃鏈緭鍏ユ鐨刋Path鎴浘

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第2张图片

鐪嬪埌浜嗙孩鍦堜綅缃笉鏄樉绀篢op Window锛岃鏄庝綘瑕佸畾浣嶇殑鐩爣鍏冪礌鍦ㄤ竴涓猧frame閲岋紝濡傛灉瑕佹搷浣滆繖涓厓绱狅紝棣栧厛闇�瑕佷粠榛樿鐨凾op Window锛屽埄鐢╯witch_to.frame('iframeid')鏂规硶鏉ュ垏鎹㈠埌鍏蜂綋鐨刬frame锛岀劧鍚庢墠鑳藉幓鎿嶄綔鐩爣鍏冪礌銆�

姝e父鐧惧害椤甸潰锛屾枃鏈緭鍏ユ鎴浘

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第3张图片

鏂规硶浠嬬粛锛�

driver.switch_to.frame("iframe1")
# 鎿嶄綔鐩爣鍏冪礌锛岃繖涓洰鏍囧厓绱犲湪 iframe1閲岄潰锛岃繖閲屽氨鏄櫨搴︽枃鏈緭鍏ユ杈撳叆鏂囧瓧
driver.switch_to.default_content() 
濡傛灉鏈夊叿浣撻」鐩〉闈㈠埄鐢ㄤ簡iframe, 鑷繁鍔ㄦ墜鍐欎竴涓嬭剼鏈��

2.3 绀轰緥浜岋細

銆�銆�瀹忓摜鏈�鍚庣粓浜庢壘鍒颁簡涓�涓悎閫傜殑demo鏉ョ粰灏忎紮浼翠滑鎴栬�呯闉嬩滑鏉ョ敤浠g爜瀹為檯婕旂粌鐨勪緥瀛愬涓嬶細

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第4张图片

姝ラ锛�

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爜瀹炵幇锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第5张图片

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爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第6张图片

2.3.4 娴忚鍣ㄨ繍琛岀粨鏋滐細

娴忚鍣ㄧ殑杩愯杩囩▼锛屽畯鍝ヨ繖閲屽氨涓嶅仛灞忓箷褰曞埗浜嗭紝杩欎釜鎴戜滑鐪嬩竴涓嬬粨鏋溿�傛祻瑙堝櫒杩愯缁撴灉濡備笅锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第7张图片

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爜瀹炵幇锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第8张图片

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爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第9张图片

娉ㄦ剰锛�
driver.switch_to_alert().accept()

杩欐槸涓�涓�佹柟娉曪紝鍦ㄦ湁浜涚紪杈戝櫒锛堝悗闈細浠嬬粛涓�娆綪ython鐨処DE宸ュ叿锛変細鎻愮ず杩欎釜鏂规硶鍒掓í绾匡紝璇存槑鍦ㄦ渶鏂癝elenium鍦≒yhton鏀寔鍖呴噷锛岃繖涓柟娉曡涓㈠純锛岃櫧鐒惰繕鏄彲浠ョ敤锛岀幇鍦ㄦ柊鐨勬柟娉曟槸switch_to.alert(),鐢ㄨ繖涓渶鏂版柟娉曪紝鎴戞祴璇曚簡涓嬶紝鏃犳硶妯℃嫙鐐瑰嚮纭畾鍜岀偣鍑诲叧

闂脊绐楁寜閽紝鎵�浠ヨ繖涓湴鏂归�夋嫨浜嗘棫鐨勬柟娉晄witch_to_alert()

4. 鑾峰彇褰撳墠椤甸潰鍏ㄩ儴鍥剧墖淇℃伅

鏈枃鏉ヤ粙缁嶄笅濡備綍鑾峰彇褰撳墠椤甸潰鍏ㄩ儴鐨勫浘鐗囦俊鎭紝鍥剧墖淇℃伅鍙兘鍖呮嫭锛屽浘鐗囧悕绉帮紝鍥剧墖澶у皬绛夈��

鐩稿叧鑴氭湰浠g爜濡備笅锛�

4.1 浠g爜瀹炵幇锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第10张图片

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爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第11张图片

杩愯缁撴灉锛�

杩愯缁撴灉锛屽彂鐜版病鏈夊浘鐗囧悕绉版墦鍗板嚭鏉ワ紝璇存槑鐧惧害鏂伴椈椤甸潰锛屾墍鏈夊浘鐗囬兘娌℃湁缁欏嚭text杩欎釜灞炴�э紝鍓嶇鐨勫瀛愭病鏈夊啓鍥剧墖鐨則ext灞炴�с�備笉鐩镐俊鐨勫悓瀛︽垨鑰呭皬浼欎即浠彲浠ラ�氳繃F12鏌ョ湅涓�涓嬪浘鐗囨槸鍚︽湁text杩欎釜灞炴�с��

5. 鑾峰彇椤甸潰鍏冪礌鐨刪ref灞炴��

鏈枃鏉ヤ粙缁嶅浣曢�氳繃Selenium鑾峰彇椤甸潰鍏冪礌鐨勬煇涓�涓睘鎬с�備竴涓厓绱犲彲鑳芥湁澶氫釜灞炴�э紝渚嬪 class, id, name, text, href, vale绛夌瓑銆傝繖閲屾垜浠妇渚嬩竴涓埇铏腑缁忓父闇�瑕佸鐞嗙殑閾炬帴闂锛氭壘鍑哄綋鍓嶉〉闈㈡墍鏈夌殑瓒呴摼鎺ャ��

宸茬櫨搴﹂椤典负渚嬶紝鎵撳嵃鎵�鏈夊寘鍚玥ref鐨勫厓绱犵殑閾炬帴銆�

鐩稿叧鑴氭湰浠g爜濡備笅锛�

5.1 浠g爜瀹炵幇锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第12张图片

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爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第13张图片

杩欓噷鍙槸鎵惧厓绱犵殑href灞炴�э紝濡傛灉浣犻渶瑕佸叾瀹冨睘鎬э紝渚嬪浣犻渶瑕佹煡鐪嬮〉闈㈡墍鏈夊厓绱犲叿鏈塱d鍊肩殑璇濓紝浣犲彲浠ヨ繖鏍峰啓锛� print (link.get_attribute('id'))

6. 濡備綍鎴浘骞朵繚瀛�

鏈枃浠嬬粛濡備綍鍒╃敤Selenium鐨勬柟娉曡繘琛屾埅鍥撅紝鍦ㄦ祴璇曡繃绋嬩腑锛屾槸鏈夊繀瑕佹埅鍥撅紝鐗瑰埆鏄亣鍒伴敊璇殑鏃跺�欒繘琛屾埅鍥俱�傚湪selenium for python涓富瑕佹湁涓変釜鎴浘鏂规硶锛屾垜浠寫閫夊叾涓渶甯哥敤鐨勪竴绉嶃��

get_screenshot_as_file()

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第14张图片

鐩稿叧浠g爜濡備笅锛�

6.1 浠g爜瀹炵幇锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第15张图片

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爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第16张图片

6.4 鎵ц缁撴灉锛�

浼氬湪妗岄潰淇濆瓨涓�寮犵櫨搴﹂椤电殑鎴浘锛屽浘鐗囧悗缂�鏄痯ng銆傛敞鎰忚矾寰勬槸瑕佷袱涓� \\ 銆傚涓嬪浘锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第17张图片

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�� 

 

鎮ㄧ殑鑲畾灏辨槸鎴戣繘姝ョ殑鍔ㄥ姏銆�濡傛灉浣犳劅瑙夎繕涓嶉敊锛屽氨璇烽紦鍔变竴涓嬪惂锛佽寰楃偣娉� 鎺ㄨ崘 涓嶈蹇樿鍝︼紒锛侊紒

 

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�_第18张图片

你可能感兴趣的:(銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�8-python+ selenium鑷姩鍖栨祴璇� -鍘嬪彴绡囷紙璇︾粏鏁欑▼锛�)