selenium之iframe与句柄

<iframe id="login_frame" name="login_frame" scrolling="no" test="jostinsu1" src="https://xui.ptlogin2.qq.com/cgi-bin/xlogin?target=self&amp;appid=522005705&amp;daid=4&amp;s_url=https://mail.qq.com/cgi-bin/readtemplate?check=false%26t=loginpage_new_jump%26vt=passport%26vm=wpt%26ft=loginpage%26target=&amp;style=25&amp;low_login=1&amp;proxy_url=https://mail.qq.com/proxy.html&amp;need_qr=0&amp;hide_border=1&amp;border_radius=0&amp;self_regurl=http://zc.qq.com/chs/index.html?type=1&amp;app_id=11005?t=regist&amp;pt_feedback_link=http://support.qq.com/discuss/350_1.shtml&amp;css=https://res.mail.qq.com/zh_CN/htmledition/style/ptlogin_input_for_xmail56dc25.css" width="100%" height="100%" frameborder="0"></iframe>

 

方法一:

# 切换iframe需要传的参数,元祖类型,也可以是一个webelement 对象,也可以是iframe的name 或者di属性
# login_frame = driver.find_element_by_id("login_frame")
# # 等待iframe出现,并切入进去
# WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it(login_frame))

方法二

frame = driver.find_element_by_id('login_frame')
driver.switch_to.frame(frame)

 

句柄:登录成功跳转

handlers = driver.window_handles # 获取所以句柄
handler = driver.current_window_handle # 获取当前句柄

driver.switch_to.window(handle) #其中handle为获取到的窗口句柄

driver.switch_to.windows(handles[0]) #切换到第一个窗口的句柄

driver.switch_to.windows(handles[-1]) #切换到最新窗口的句柄

 

all_handlers = driver.window_handles # 获取所以句柄

handler = driver.current_window_handle # 获取登录后的句柄
for handler in all_handlers:
if handler != login_handler:
driver.switch_to_window(handler)

 

posted @ 2021-07-04 21:40  书院小先生  阅读(216)  评论(0编辑  收藏  举报