Python webdriver.Chrome()的使用解读

 更新时间:2023年02月25日 09:20:17   作者:HHT0506  
这篇文章主要介绍了Python webdriver.Chrome()的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

webdriver.Chrome()的使用

1.前提

Python与Chrome路径下均安装chromedriver.exe

2.chromedriver.exe版本选择及下载

chromedriver.exe版本需要与浏览器版本一致:

3.安装

下载后解压,将 chromedriver.exe复制到下面两个目录中:

  • Chrome目录:比如C:\Program Files (x86)\Google\Chrome\Application
  • Python目录:比如D:\Softwares\Python39

4.添加环境变量

将上述Chrome路径添加进系统环境光变量,Python使用时应该加入环境变量了,这个就不用管了。

5.测试代码

import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
driver.find_element_by_id("kw").send_keys(u"胡皓天")
driver.find_element_by_id("su").click()

webdriver.Chrome参数解释

通过源码看解释

最直接最不讲道理的方式就是看源码,使用之前一定要学会看说明书。

打算使用jupyter演示的,但是jupyter不支持看源码,所以使用Pycharm吧。

from selenium import webdriver

Broswer = webdriver.Chrome(executable_path="chromedriver", port=0,
                 options=None, service_args=None,
                 desired_capabilities=None, service_log_path=None,
                 chrome_options=None, keep_alive=True)
print(webdriver.Chrome.__doc__)
"""
 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
"""
print(webdriver.Chrome.__init__.__doc__)
"""
Creates a new instance of the chrome driver.

        Starts the service and then creates new instance of chrome driver.

        :Args:
         - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
         - port - port you would like the service to run, if left as 0, a free port will be found.
         - options - this takes an instance of ChromeOptions
         - service_args - List of args to pass to the driver service
         - desired_capabilities - Dictionary object with non-browser specific
           capabilities only, such as "proxy" or "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.
"""

Chrome Doc解释: 控制ChromeDriver并允许您驱动浏览器。

你需要从http://chromedriver.storage.googleapis.com/index.html下载ChromeDriver的可执行文件。

没有下ChromeDriver的小伙伴根据自己浏览器版本下载相应的ChromeDriver版本,两者版本关系ChromeDriver版本>=Chrome版本。

参数解释:

  • executable_path - 可执行文件的路径。如果使用默认值,则假定可执行文件位于$PATH中。
  • port- 你希望服务运行的端口,如果为0,使用空闲端口。
  • options - 这是ChromeOptions的一个实例
  • service_args - 要传递给驱动程序服务的args列表
  • desired_capabilities -仅具有非浏览器特定功能的字典对象,例如“proxy”或“loggingPref”。
  • service_log_path - 记录来自驱动程序的信息存放路径。
  • chrome_options - chrome选项。
  • keep_alive -是否配置ChromeRemoteConnection使用HTTP keep-alive。

其中options和chrome_options:使用options代替chrome_options。

 if chrome_options:
            warnings.warn('use options instead of chrome_options',
                          DeprecationWarning, stacklevel=2)
            options = chrome_options

并且告知此警告被弃用

关于executable_path解释:value是ChromeDriver.exe路径。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 解决django框架model中外键不落实到数据库问题

    解决django框架model中外键不落实到数据库问题

    这篇文章主要介绍了解决django框架model中外键不落实到数据库问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • Python matplotlib实现多子图布局

    Python matplotlib实现多子图布局

    多子图布局是指在一个图像中同时显示多个子图,每个子图可以是独立的图形或者是相互关联的图形,下面我们就来了解下matplotlib是如何实现多子图布局的吧
    2023-12-12
  • python用pd.read_csv()方法来读取csv文件的实现

    python用pd.read_csv()方法来读取csv文件的实现

    本文主要介绍了python用pd.read_csv()方法来读取csv文件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • YOLOv5以txt或json格式输出预测结果的方法详解

    YOLOv5以txt或json格式输出预测结果的方法详解

    这篇文章主要给大家介绍了关于YOLOv5以txt或json格式输出预测结果的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2023-03-03
  • Django nginx配置实现过程详解

    Django nginx配置实现过程详解

    这篇文章主要介绍了Django nginx配置实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • 基于python实现智能用例生成工具

    基于python实现智能用例生成工具

    这篇文章主要为大家详细介绍了如何基于python实现智能用例生成工具,即根据输入的功能点,生成通用测试点,感兴趣的小伙伴可以跟随小编一起了解下
    2023-09-09
  • python多线程socket编程之多客户端接入

    python多线程socket编程之多客户端接入

    这篇文章主要为大家详细介绍了python多线程socket编程之多客户端接入,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • Python中Merge使用的示例详解

    Python中Merge使用的示例详解

    Python里的merger函数是数据分析工作中最常见的函数之一,类似于MySQL中的join函数和Excel中的vlookup函数。本文将通过一些简单的实力和大家聊聊Merge的使用,需要的可以了解一下
    2023-02-02
  • Python实现绘制多角星实例

    Python实现绘制多角星实例

    这篇文章要给大家分享Python实现绘制多角星的实例,在具备一定的Python编程基础以后,我们可以结合for循环进行多角星的编写,只要简单的几次循环,即可以极大的解决重复编写相同代码方面的问题,下面小编将以三角星,五角星为例,进而引入如何绘制多角星,需要的朋友可以参考一下
    2021-11-11
  • Python中将字典转换为列表的方法

    Python中将字典转换为列表的方法

    这篇文章主要介绍了Python中将字典转换为列表的方法,需要的朋友可以参考下
    2016-09-09

最新评论