iframe 切换

谁说我不能喝 提交于 2019-12-20 20:55:20

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

-------------------------------------------------推荐使用方法二 稳!----------------------------------------------------

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!