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()

原文地址:https://www.cnblogs.com/wsk1988/p/12692540.html