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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM