Python Selenium 之关闭窗口close与quit的方法

yipeiwu_com6年前Python基础

selenium关闭窗口有两个方法,close与quit,我们稍作研究便知道这两个方法的区别。

1.看源码或API

这是close()的说明:

Closes the current window. 
关闭当前窗口。

这是quit()的说明:

Quits the driver and closes every associated window. 
退出驱动并关闭所有关联的窗口。

从这里就很明显的看出来这两个方法的区别,一个关闭当前窗口,一个关闭所有窗口,下面写一小段代码测试一下。

2.代码试验

代码:

# -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
driver.get('http://sahitest.com/demo/index.htm')
print driver.current_window_handle # 查看当前window handle

driver.find_element_by_link_text('Window Open Test').click() # 打开新window1
driver.find_element_by_link_text('Window Open Test With Title').click() # 打开新window2
print driver.window_handles # 查看所有window handles

driver.close()
print driver.window_handles # 查看现在的所有window handles,可看到只是关闭了最开始的一个window,其他两个window还在

driver.quit() # 看到所有window都被关闭

结果:

{b030dd54-3cbd-4d7b-800a-2ff296f03f5b}
[u'{b030dd54-3cbd-4d7b-800a-2ff296f03f5b}', u'{7fdacf2e-0c34-4f0d-9a7a-ae34f3af932c}', u'{f2d79121-8cc2-47ea-bd7d-2035e305ba2f}']
[u'{7fdacf2e-0c34-4f0d-9a7a-ae34f3af932c}', u'{f2d79121-8cc2-47ea-bd7d-2035e305ba2f}']
<link rel="stylesheet" href="http://csdnimg.cn/release/phoenix/production/markdown_views-10f5517761.css" rel="external nofollow" >
</div>

以上这篇Python Selenium 之关闭窗口close与quit的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python数据类型学习笔记

Python数据类型学习笔记

带你走进数据类型 一:整数、浮点数 Python中整数和浮点数的定义以及运算和C++都是一样的,我在这里就不需多说了,我就说明一点:Python相对于C/C++而言,定义整数没有int...

python django 增删改查操作 数据库Mysql

下面介绍一下django增删改查操作: 1、view.py # -*- coding: utf-8 -*- from __future__ import unicode_litera...

彻底搞懂Python字符编码

彻底搞懂Python字符编码

不论你是有着多年经验的 Python 老司机还是刚入门 Python 不久,你一定遇到过UnicodeEncodeError、UnicodeDecodeError 错误,每当遇到错误我们...

Python获取基金网站网页内容、使用BeautifulSoup库分析html操作示例

本文实例讲述了Python获取基金网站网页内容、使用BeautifulSoup库分析html操作。分享给大家供大家参考,具体如下: 利用 urllib包 获取网页内容 #引入包 fr...

关于Python中异常(Exception)的汇总

前言 Exception类是常用的异常类,该类包括StandardError,StopIteration, GeneratorExit, Warning等异常类。python中的异常使用...