WebUI_句柄和iframe

from selenium import webdriver
from selenium.webdriver.common.by import By

# 创建 WebDriver 对象
wd = webdriver.Chrome()

# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
wd.get('https://www.byhy.net/_files/stock1.html')

# frame/iframe 操作
# 切换到iframe中
wd.switch_to.frame('xx') # xx为iframe或frame的id/class
# 切换到iframe外层 切进来记得切出去
wd.switch_to.default_content()


# mainWindow变量保存当前窗口的句柄
mainWindow = wd.current_window_handle
# 切换到新窗口
# driver.switch_to.window()
for handle in wd.window_handles:
# 先切换到该窗口
wd.switch_to.window(handle)
# 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口
if 'Bing' in wd.title:
# 如果是,那么这时候WebDriver对象就是对应的该该窗口,正好,跳出循环,
break
# 通过前面保存的老窗口的句柄,自己切换回老窗口
wd.switch_to.window(mainWindow)
posted @ 2022-06-16 17:23  非同凡响  阅读(32)  评论(0编辑  收藏  举报