html tool

2015年2月11日星期三

selenium 窗口切换python

add:http://www.cnblogs.com/Ralph-Wang/p/3393401.html

---------------------------------------
# 点击链接,打开新窗口
l = dr.find_element_by_xpath("//body/a")
l.click()

old_handle = dr.current_window_handle
[popexizhi:保存旧的窗口句柄]
for handle in dr.window_handles:
    if old_handle != handle:
        new_handle = handle
        break

# 切换到新窗口中,点击新窗口中的按钮
dr.switch_to_window(new_handle)
btn = dr.find_element_by_xpath('//input[@id="b1"]')
btn.click()

# 关闭alert
alert = dr.switch_to_alert()
alert.accept()

sleep(3)
dr.close()
# 切回到原页面关闭
dr.switch_to_window(old_handle)
[popexizhi:这里才是重要的,但使用时有切换不回来的情况,还是偶发的why??]
dr.close()


没有评论:

发表评论