一、frame的定位方式
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.get('https://mail.126.com/')
driver.implicitly_wait(10)
'''frame的幾種定位方式,
第一種,frame有id或者有name則使用,switch_to.frame只能用於iframe存在id或者name
driver.switch_to.frame('x-URS-iframe')
driver.find_element_by_name('email').send_keys('12355o6')
'''
'''第二種,無id和name
對iframe像普通元素那樣先查找到,然后切換
iframe = driver.find_element_by_id('x-URS-iframe')
driver.switch_to.frame(iframe)
driver.find_element_by_name('email').send_keys('12355o6')
'''
'''第三種, 直接數topwin中有多少個iframe,按照數組的方式找到,也就是通過索引值找到
,弊端,不知道要找的iframe的索引是多少
WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id('x-URS-iframe'))
driver.switch_to.frame(0) #括號中為iframe所在的索引位置
driver.find_element_by_name('email').send_keys('12355o6')
備注:進入iframe后如果要操作ifram外面的元素,需要先退出
driver.switch_to.default_content() #跳出到topwindown層
二、多個frame的問題
'''iframe嵌套問題
第一種:
topwindow中有2個iframe(f1、f2)並列
driver.switch_to.frame('f1') #跳到f1的iframe層
driver.switch_to.default_content() #跳出f1,進入topwindow層
driver.switch_to.frame('f2') #進入f2的iframe層
第二種:
topwindow中有2個iframe(f1、f2),f2在f1里面
driver.switch_to.frame('f1') #從topwindow中進入f1
driver.switch_to.frame('f2') #從f1中進入f2
driver.switch_to.default_content() #退出f2,直接進入topwindow
driver.switch_to.frame('f1') #從topwindow中進入f1
driver.switch_to.frame('f2') #從f1中進入f2
driver.switch_to.parent_frame() #退回到父目錄
'''