from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
1.先找到你要切换的 iframe 的元素
iframe元素 = ‘//iframe[@name=“login_frame_qq”]’
#方法一
driver.switch_to.frame(driver.find_element_by_xpath(’//iframe[@name=“login_frame_qq”]’))
#这个时候进入成功了
下面开始在iframe页面里进行元素操作
driver.find_element_by_id(“switcher_plogin”).click()
#方法二
#检测iframe是否可用,然后再切换进来
#用WebDriverWait().until(EC.frame_to_be_available_and_switch_to_it(这个里面传元素定位))
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_xpath(’//iframe[@name=“login_frame_qq”]’)))
#下面开始在iframe页面里进行元素操作
driver.find_element_by_id(“switcher_plogin”).click()
#从iframe当中回到默认页面当中 default_content()
driver.switch_to.default_content()
#这个是跳回父级页面 parent_frame() 除非多个iframe才用得到 特别少
driver.switch_to.parent_frame()
-------------------------------------------------推荐使用方法二 稳!----------------------------------------------------
来源:CSDN
作者:何凤祥在努力中
链接:https://blog.csdn.net/weixin_44954642/article/details/103629907