Selenium(二):打开和关闭浏览器


一、知识点:

webdriver.Chrome()
webdriver.Safari()
webdriver.Firefox()
webdriver.Ie()
webdriver.Edge()
webdriver.Opera()
close()
quit()

二、示例

示例1:打开浏览器.py

# encoding:utf-8
from selenium import webdriver

# 1、打开 谷歌浏览器
driver = webdriver.Chrome()

# 2、打开 Safari浏览器
driver = webdriver.Safari()

# 3、打开 火狐浏览器
driver = webdriver.Firefox()

# 4、打开 IE浏览器
driver = webdriver.Ie()

# 5、打开 Edge浏览器
driver = webdriver.Edge()

# 6、打开 欧朋浏览器
driver = webdriver.Opera()

### 示例2:关闭浏览器窗口 ``` # encoding:utf-8 from selenium import webdriver from time import sleep

打开 谷歌浏览器

driver = webdriver.Chrome()

打开拉钩职位列表

driver.get('https://www.lagou.com/zhaopin/xiaoshoujingli1/?labelWords=label')
sleep(3)

查看职位

driver.find_element_by_css_selector('.p_top a h3').click()
sleep(2)

关闭窗口

driver.close()

<br>
### 示例3:关闭浏览器和所有关联窗口--结束浏览器进程.py

encoding:utf-8

from selenium import webdriver
from time import sleep

打开 谷歌浏览器

driver = webdriver.Chrome()

打开拉钩职位列表

driver.get('https://www.lagou.com/zhaopin/xiaoshoujingli1/?labelWords=label')
sleep(3)

查看职位

driver.find_element_by_css_selector('.p_top a h3').click()
sleep(2)

关闭浏览器和所有关联窗口--结束浏览器进程

driver.quit()




<br>


### 三、相关源码:
#### \site-packages\selenium\webdriver\chrome\webdriver.py

class WebDriver(RemoteWebDriver):
"""
Controls the ChromeDriver and allows you to drive the browser.

You will need to download the ChromeDriver executable from
http://chromedriver.storage.googleapis.com/index.html
"""

def __init__(self, executable_path="chromedriver", port=0,
    options=None, service_args=None,
    desired_capabilities=None, service_log_path=None,
    chrome_options=None, keep_alive=True):

"""
Creates a new instance of the chrome driver.
创建chrome驱动程序的新实例
Starts the service and then creates new instance of chrome driver.
启动该服务,然后创建新的chrome驱动程序实例

:Args:
- executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
可执行文件的路径。如果使用默认值,则假定可执行文件位于$ PATH中

- port - port you would like the service to run, if left as 0, a free port will be found.
您希望服务运行的端口,如果保留为0,将找到一个空闲端口。

- options - this takes an instance of ChromeOptions
这需要一个ChromeOptions实例

- service_args - List of args to pass to the driver service
传递给驱动程序服务的args列表

- desired_capabilities - Dictionary object with non-browser specific
capabilities only, such as "proxy" or "loggingPref".
仅具有非浏览器特定功能的目标对象,例如“proxy”或“loggingPref”。

- service_log_path - Where to log information from the driver.

从驱动程序记录信息的位置

- chrome_options - Deprecated argument for options
选项的弃用参数

- keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
是否将ChromeRemoteConnection配置为使用HTTP keep-alive

"""

<br>
#### \site-packages\selenium\webdriver\remote\webdriver.py

def close(self):
"""
Closes the current window.
关闭当前窗口

:Usage:
driver.close()
"""
    self.execute(Command.CLOSE)

def quit(self):
"""
Quits the driver and closes every associated window.
退出驱动程序并关闭每个关联的窗口 -- 关闭浏览器进程
:Usage:
driver.quit()
"""
    try:
    self.execute(Command.QUIT)
    finally:
    self.stop_client()

<br>
<br>
<br>
<br>
<br>
<br>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM