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

yipeiwu_com5年前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 itertools模块详解

这货很强大, 必须掌握 文档 链接 http://docs.python.org/2/library/itertools.html pymotw 链接 http://pymotw.com...

python二元表达式用法

二元表达式: wide=1 new_w = 299 if not wide else 28 print(new_w) new_w = 299 if wide>0 else 28...

用Python抢过年的火车票附源码

用Python抢过年的火车票附源码

前言:大家跟我一起念,Python大法好,跟着本宝宝用Python抢火车票 首先我们需要splinter 安装: pip install splinter -i http://pyp...

Python操作qml对象过程详解

1. 如何在python里获得qml里的对象? 1.1 获取根对象 QML: import QtQuick 2.12 import QtQuick.Controls 2.12 A...

TensorFlow——Checkpoint为模型添加检查点的实例

TensorFlow——Checkpoint为模型添加检查点的实例

1.检查点 保存模型并不限于在训练模型后,在训练模型之中也需要保存,因为TensorFlow训练模型时难免会出现中断的情况,我们自然希望能够将训练得到的参数保存下来,否则下次又要重新训练...