iframe切换

'''
frame 框架里面元素定位
frame:一个页面里面嵌套了另外一个框架页面
切换方法 driver.switch_to.frame()
重新切换到主页操作:driver.switch_to.default_content()
例子:腾讯课堂登录切换到输入用户名/密码登录
frame框架名:login_frame_qq


'''
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
import time
qq = webdriver.Chrome()
qq.get("https://ke.qq.com/")
qq.find_element_by_xpath('//a[@id="js_login"]').click()

time.sleep(3)
qq.switch_to.alert#切换alert弹框
# qq.find_element_by_xpath('//i[@class="icon-font i-qq"]').click()
qq.find_element_by_xpath('//a[@class="js-btns-enter btns-enter btns-enter-qq"]').click()
#等待要切换的iframe页面出现,并且换进入iframe
WebDriverWait(qq,10).until(EC.frame_to_be_available_and_switch_to_it('login_frame_qq'))
# qq.switch_to.frame('login_frame_qq')#切换到name属性切换iframe
# qq.switch_to.frame(4)#下标切换方式
# qq.switch_to.frame(qq.find_element_by_name('login_frame_qq'))
time.sleep(3)
qq.find_element_by_xpath('//a[@id="switcher_plogin"]').click()
time.sleep(3)
qq.switch_to.default_content()#重新切换到主页操作

qq.find_element_by_id('login_close').click()

qq.quit()
posted @ 2019-07-03 15:17  北方Test  阅读(1420)  评论(0编辑  收藏  举报