7

Selenium4+Python3系列(五) - 多窗口处理之句柄切换 - 久曲健

 2 years ago
source link: https://www.cnblogs.com/longronglang/p/16842063.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

感觉到很惭愧呀,因为居然在Selenium+Java系列中没有写过多窗口处理及句柄切换的文章,不过也无妨,不管什么语言,其思路是一样的,下面我们来演示,使用python语言来实现窗口句柄的切换。

什么是窗口句柄

可能有的同学会问了,到底啥是窗口,什么又是多窗口,句柄到底是啥玩楞?

那么,他到底长啥样?如下图所示:

718867-20221030190541305-1201480316.png

怎么处理多窗口句柄

如何获取窗口句柄

语法:

driver.current_window_handle

1、模拟场景

打开搜狗首页,输入淘宝进入久曲健 博客园,并输出页面标题

2、使用具柄判断切换

就是通过窗口句柄判断切换,示例代码入下:

python
for window_handle in window_handles:
    if window_handle != old_Window:
        driver.switch_to.window(window_handle)
        print(driver.title)
    else:
        driver.close()

3、使用URL判断切换

就是根据跳转的当前URL切换,示例代码如下:

python
for window_handle in window_handles:
    driver.switch_to.window(window_handle)
    newUrl = driver.current_url
    if 'cnblogs' in newUrl:
        print(driver.title)
    else:
        driver.close()

4、通过下标切换

window_handles = driver.window_handles返回的是一个集合,所以自然用下标去切换,简单点说就是,只要找到想要切换的窗口下标即可实现切换,多个窗口不建议使用此方法。

示例代码如下:

python
driver.close()
driver.switch_to.window(window_handles[1])
print(driver.title)

5、关闭新窗口句柄,切回原来主页

笔者反复试验发现,用句柄删除不好用,还是这个url靠谱,示例代码如下:

python
for window_handle in window_handles:
    driver.switch_to.window(window_handle)
    newUrl = driver.current_url
    if 'cnblogs' in newUrl:
        driver.close()
    else:
        print(driver.title)

所有汇总代码如下:

python
# -*- coding: utf-8 -*-
"""
@Time : 2022/10/26 14:12
@Auth : 软件测试君
@File :switch_window.py
@IDE :PyCharm
@Motto:ABC(Always Be Coding)
多窗口处理之句柄切换

"""
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

'''
初始化操作
'''
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))


def init():
    # 最大化操作
    driver.maximize_window()
    driver.set_script_timeout(60)
    # 智能等待找到元素后立即继续执行,全局生效
    driver.implicitly_wait(60)
    driver.set_page_load_timeout(60)


init()

driver.get("https://www.sogou.com/")
driver.find_element(By.ID, "query").clear()
driver.find_element(By.ID, "query").send_keys("久曲健 博客园")
driver.find_element(By.ID, "stb").click()
time.sleep(2)
# 获取当前页面窗口句柄(每个句柄的表示都是唯一的)
old_Window = driver.current_window_handle
print('当前窗口句柄为:{0}'.format(old_Window))
driver.find_element(By.LINK_TEXT, "久曲健 - 博客园").click()
time.sleep(2)
window_handles = driver.window_handles
print(window_handles)
'''
### 方法一:
for window_handle in window_handles:
    if window_handle != old_Window:
        driver.switch_to.window(window_handle)
        print(driver.title)
    else:
        driver.close()

'''
'''
### 方法二:
driver.close()
driver.switch_to.window(window_handles[1])
print(driver.title)

'''
'''
### 方法三:
for window_handle in window_handles:
    driver.switch_to.window(window_handle)
    newUrl = driver.current_url
    if 'cnblogs' in newUrl:
        print(driver.title)
    else:
        driver.close()
'''

# 关闭新窗口句柄,切回原来主页
for window_handle in window_handles:
    driver.switch_to.window(window_handle)
    newUrl = driver.current_url
    if 'cnblogs' in newUrl:
        driver.close()
    else:
        print(driver.title)

driver.quit()

最近心情还是很不好,恐慌的不是单身可怕,而是年纪越来越大,也不知道现在的女孩都在想什么,好想结婚呀!

感兴趣的同学,请自己去尝试,关于Selenium4.0+Python3系列教程,未完待续····

__EOF__


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK