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

yipeiwu_com7年前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 重命名轴索引的方法

如下所示: import numpy as np from pandas import Series, DataFrame ###重命名轴索引 data = DataFrame(np...

Python 获取div标签中的文字实例

预备知识点 compile 函数 compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用。 语法...

matplotlib简介,安装和简单实例代码

matplotlib简介,安装和简单实例代码

官网介绍: Matplotlib is a Python 2D plotting library which produces publication quality figures i...

Python实现微信好友的数据分析

Python实现微信好友的数据分析

基于微信开放的个人号接口python库itchat,实现对微信好友的获取,并对省份、性别、微信签名做数据分析。 效果: 直接上代码,建三个空文本文件stopwords.txt,ne...

Python星号*与**用法分析

Python星号*与**用法分析

本文实例分析了Python星号*与**用法。分享给大家供大家参考,具体如下: 1. 加了星号(*)的变量名会存放所有未命名的变量参数,不能存放dict,否则报错。 如: def mu...