web自動化中的三種切換---iframe


1、iframe切換方式一

  • 首先先確定定位元素在iframe元素中,打開網頁https://ke.qq.com/點擊登錄按鈕,按F12,定位賬戶密碼登錄元素

  

 

  • 切換到指定的iframe元素中,如下   
 1 from selenium import webdriver
 2 from selenium.webdriver.support.wait import WebDriverWait
 3 from selenium.webdriver.support import expected_conditions as EC
 4 from selenium.webdriver.common.by import By
 5 import time
 6 
 7 driver=webdriver.Chrome()
 8 # 訪問一個網頁
 9 driver.get("https://ke.qq.com/")
10 driver.maximize_window()
11 driver.find_element_by_id('js_login').click()
12 # 等待//div[@class='ptlogin-wrap']//i[@class='icon-font i-qq']元素出現
13 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[@class='ptlogin-wrap']//i[@class='icon-font i-qq']")))
14 # 點擊//div[@class='ptlogin-wrap']//i[@class='icon-font i-qq']元素
15 driver.find_element_by_xpath("//div[@class='ptlogin-wrap']//i[@class='icon-font i-qq']").click()
16 # 等待iframe元素出現
17 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.NAME,"login_frame_qq")))
18 # 切換iframe
19 driver.switch_to.frame('login_frame_qq') #name屬性方式定位
20 time.sleep(0.5)#切換到iframe元素過程等待0.5秒
21 # 等待id為switcher_plogin的元素出現
22 WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,'switcher_plogin')))
23 driver.find_element_by_id('switcher_plogin').click()
24 # Switches focus to the specified frame, by index, name, or webelement.
25 '''
26 :Usage:
27             driver.switch_to.frame('frame_name')#通過name屬性
28             driver.switch_to.frame(1)#通過index  從1開始
29             driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])#通過其他元素定位方式切換iframe  find_elements_by_tag_name
30 '''
31 # driver.switch_to.frame(1)#index方式定位
32 # driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@name="login_frame_qq"]'))#其他定位元素的方式,xpath方式定位

2、iframe切換方式二 

# iframe方式二切換,#直接在WebDriverWait中使用函數frame_to_be_available_and_switch_to_it,切換到iframe中
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it('login_frame_qq'))
#等待0.5秒 time.sleep(
0.5)

 3、從iframe當中回到默認的頁面當中 driver.switch_to.default_content() 

 1 from selenium import webdriver
 2 from selenium.webdriver.support.wait import WebDriverWait
 3 from selenium.webdriver.support import expected_conditions as EC
 4 from selenium.webdriver.common.by import By
 5 import time
 6 
 7 driver=webdriver.Chrome()
 8 # 訪問一個網頁
 9 driver.get("https://ke.qq.com/")
10 driver.maximize_window()
11 driver.find_element_by_id('js_login').click()
12 # 等待//div[@class='ptlogin-wrap']//i[@class='icon-font i-qq']元素出現
13 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[@class='ptlogin-wrap']//i[@class='icon-font i-qq']")))
14 # 點擊//div[@class='ptlogin-wrap']//i[@class='icon-font i-qq']元素
15 driver.find_element_by_xpath("//div[@class='ptlogin-wrap']//i[@class='icon-font i-qq']").click()
16 # 等待iframe元素出現
17 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.NAME,"login_frame_qq")))
18 # # 切換iframe
19 driver.switch_to.frame('login_frame_qq') #name屬性方式定位
20 time.sleep(0.5)#切換到iframe元素過程等待0.5秒
21 # 等待id為switcher_plogin的元素出現
22 WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,'switcher_plogin')))
23 driver.find_element_by_id('switcher_plogin').click()
24 # Switches focus to the specified frame, by index, name, or webelement.
25 '''
26 :Usage:
27             driver.switch_to.frame('frame_name')#通過name屬性
28             driver.switch_to.frame(1)#通過index  從1開始
29             driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])#通過其他元素定位方式切換iframe  find_elements_by_tag_name
30 '''
31 
32 # driver.switch_to.frame(1)#index方式定位
33 # driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@name="login_frame_qq"]'))#其他定位元素的方式,xpath方式定位
34 
35 # iframe方式二切換,#直接在WebDriverWait中使用函數frame_to_be_available_and_switch_to_it,切換到iframe中
36 # WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it('login_frame_qq'))
37 # time.sleep(0.5)
38 
39 # 從iframe當中回到默認的頁面當中
40 driver.switch_to.default_content()
41 time.sleep(1)
42 driver.find_element_by_xpath("//a[@id='login_close']//i[@class='icon-font i-close']").click()
43 driver.find_element_by_id('js_login').click()

備注:iframe只能一層一層的切進去,不能跳級切換

# 跳到上一級的iframe中,使用以下方法
driver.switch_to.parent_frame()
 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM