温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python webdriver.Chrome()如何使用

发布时间:2023-02-25 10:38:21 来源:亿速云 阅读:140 作者:iii 栏目:开发技术

这篇文章主要介绍“Python webdriver.Chrome()如何使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Python webdriver.Chrome()如何使用”文章能帮助大家解决问题。

    webdriver.Chrome()的使用

    1.前提

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

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

    • Chrome版本查看:浏览器右上角三个点->帮助->关于Google Chrome

    Python webdriver.Chrome()如何使用

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

    Python webdriver.Chrome()如何使用

    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路径。

    关于“Python webdriver.Chrome()如何使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI