目录
1.切换操作
(1)Iframe问题
frame元素或者iframe元素的内部 会包含一个
被嵌入的
另一份html文档,导致Selenium无法调用find_element方法获取Iframe元素
![](https://img-blog.csdnimg.cn/6f6b4c47b35d4321a400b49c94370b5f.bmp)
(2)selenium解决Iframe访问
-
切换IFrame:driver.switch_to.frame( frame元素的属性 name、ID或WebElement对象)。若有多个Iframe嵌套则需要多次切换
-
再返回主页面: wd.switch_to.default_content()返回最外面/主Html
(3)新窗口问题
# 点击打开新窗口的链接
driver.find_element(By.TAG_NAME, "a").click()
# 问题:wd.title属性是当前窗口的标题栏文本,并不是微软bing
print(wd.title)
![](https://img-blog.csdnimg.cn/00a9ee8d60cc4d0b923c9c338dc06e5f.png)
(4)Selenium解决窗口切换
可以使用Webdriver对象的
switch_to属性的window方法:
driver
.switch_to.window(handle)
-
handle定义: WebDriver对象有window_handles 属性,是列表对象, 里面包括了当前浏览器里面 所有的窗口句柄,即每个窗口对应唯一ID。
![](https://img-blog.csdnimg.cn/daeafdff547043269e4be48e0ade20b7.png)
-
回主窗口方式:
-
①事先保留handle值
-
![](https://img-blog.csdnimg.cn/7e72caa91fdf4f93a61f1fba33df1c42.png)
-
②跳回handle
![](https://img-blog.csdnimg.cn/d4a5b9ee4de84b05823b3ae573653958.png)