一、获取当前句柄及所有句柄

handle=driver.current_window_handle  #获取当前窗口句柄
print(handle)
handles=driver.window_handles #获取所有窗口句柄
print(handles)

二、获取指定句柄,并封装成方法

#coding=gbk
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select

curent_path=os.path.dirname(__file__)
driver=webdriver.Chrome()
page_path=os.path.join(curent_path,'./pages/element_samples.html')
driver.implicitly_wait(10)
driver.get('file://'+page_path)

handle=driver.current_window_handle #获取当前窗口句柄
print(handle)
handles=driver.window_handles #获取所有窗口句柄
print(handles)

##封装
def switch_window_by_title(title):
for handle in driver.window_handles:
driver.switch_to.window(handle)
if driver.title.__contains__(title):
break

def switch_window_by_url(url):
for handle in driver.window_handles:
driver.switch_to.window(handle)
if driver.current_url.__contains__(url):
break

##小测试

e=driver.find_element(By.XPATH,'//select[@name="jumpMenu"]')
Select(e).select_by_visible_text("开封教育网")
# switch_window_by_title("开封市教育体育网") ##切换到这个句柄
switch_window_by_url('http://jtj.kaifeng.gov.cn/')
driver.find_element(By.XPATH,'//a[text()="政务服务"]').click()
												

Python-selenium,切换句柄及封装的更多相关文章

  1. Python_Selenium之basepage 识别元素、浏览器操作、获取属性、鼠标事件、键盘事件、弹窗、切换frame、切换句柄等封装

    #coding=gbkimport osimport timefrom selenium import webdriverfrom selenium.webdriver.common.by impor ...

  2. python+selenium切换窗口(获取句柄信息)

    current_window_handle:获得当前窗口句柄: window_handles:返回所有窗口的句柄到当前会话: switch_to.window(suoyou[0]) ========= ...

  3. Python&Selenium智能等待方法封装

    摘要:本篇博文用几行代码展示Python和Selenium做自动化测试时常见的显示等待和封装 # 用于实现智能等待页面元素的出现 # encoding = utf-8 ""&quo ...

  4. python selenium基于显示等待封装的一些常用方法

    import os import time from PIL import Image from selenium import webdriver from appium import webdri ...

  5. Python+Selenium中级篇之-封装一个自己的类-浏览器引擎类

    前一篇文章我们知道了,如何去封装几个简单的Selenium方法到我们自定义的类,这次我们编写一个类,叫浏览器引擎类,通过更改一个字符串的值,利用if语句去判断和控制启动那个浏览器.这里我们暂时,支持三 ...

  6. Selenium 切换句柄

    最近用了网络上别人的一段切换窗口的code每次成功了,不错,学习 // 根据Title切换新窗口 public boolean switchToWindow_Title(WebDriver drive ...

  7. python+selenium 切换至iframe

    方法一: from selenium import webdriver driver = webdriver.Firefox() driver.switch_to.frame(0) # 1.用fram ...

  8. python+selenium五:多窗口切换与获取句柄

    from selenium import webdriverfrom selenium.webdriver.common.by import Byimport time driver = webdri ...

  9. python+selenium十:selenium的二次封装

    python+selenium十:基于原生selenium的二次封装   from selenium import webdriverfrom selenium.webdriver.support.w ...

随机推荐

  1. metasploit console 命令解释

    折腾几天,总算是在虚拟机中安装好了4.2版本的metasploit,能够成功打开console,这里将metasploit console的指令用中文翻一下: 原文及翻译: back Move bac ...

  2. Redis数据结构—链表与字典的结构

    目录 Redis数据结构-链表与字典的结构 链表 Redis链表节点的结构 Redis链表的表示 Redis链表用在哪 字典 Redis字典结构总览 Redis字典结构分解 Redis字典的使用 Re ...

  3. 【JavaScript】Leetcode每日一题-移除元素

    [JavaScript]Leetcode每日一题-移除元素 [题目描述] 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度. 不要使用 ...

  4. [LeetCode每日一题]88. 合并两个有序数组

    [LeetCode每日一题]88. 合并两个有序数组 问题 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组. 初始化 n ...

  5. 鼠标右键添加vscode

    实现效果 右键文件夹,可以使用vscode打开 右键文件,可以使用vscode打开 右键空白处,可以使用vscode打开 进行实现 下载压缩包 为了方便操作,和减少错误,将.reg文件上传到网盘中,点 ...

  6. angularjs中的常遇问题整理

    1.页面中默认图片的显示 {{img || "/upload/img.png"}} 2.接口中的数据没有及时读取到,$apply $.post( url, {}, function ...

  7. spring中注解@Resource 与@Autowire 区别

    ① .@Resource 是根据名字进行自动装配:@Autowire是通过类型进行装配. ②. @Resource 注解是 jdk 的:@Autowire 是spring的.

  8. QFNU-11.08training

    7-1  阅览室 题目: 天梯图书阅览室请你编写一个简单的图书借阅统计程序.当读者借书时,管理员输入书号并按下S键,程序开始计时:当读者还书时,管理员输入书号并按下E键,程序结束计时.书号为不超过10 ...

  9. mitmproxy 获取请求响应数据

    比较好的一个介绍是:https://blog.wolfogre.com/posts/usage-of-mitmproxy/ mitproxy 获取请求响应数据的代码如下: # -*- coding: ...

  10. Spring的Xml和JavaConfig 扩展你选哪一个?

    引言 上一篇文章我们有怎么介绍到如何通过XML的形式来定义Spring的扩展<Spring面试高频题如何:自定义XML schema 扩展>,好多人都在吐槽现在都什么年代了,xml还有人再 ...