• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Python Selenium 驱动下载和配置使用详细流程

武飞扬头像
卡尔特斯
帮助5

1、安装

$ pip install selenium

2、下载浏览器驱动(webdriver

3、使用

  • 代码跑起后,没有写关闭浏览器的代码,运行几秒后,浏览器自动关闭的问题解决

  • 验证浏览器驱动是否正常使用,确保 python 环境正常,selenium 包已经安装。

    # 导入
    from selenium import webdriver
    # 如果需要指定路径,但是路径在新版本中被重构到 Service 函数中了
    # from selenium.webdriver.chrome.service import Service
    # 延时器
    from time import sleep
    
    # 1、浏览器驱动路径
    # win_path = 'chromedriver.exe'
    # mac_path = 'chromedriver'
    
    # 2、直接传入字符串路径报错:DeprecationWarning: executable_path has been deprecated, please pass in a Service object
    # 原因是这种写法将放弃使用
    # driver = webdriver.Chrome(mac_path)
    
    # 2、需要使用 Service 进行包裹一下【推荐】
    # options = webdriver.ChromeOptions()
    # options.add_experimental_option('detach', True) # 不自动关闭浏览器
    # service = Service(mac_path)
    # driver = webdriver.Chrome(service=service, options=options)
    
    # 创建浏览器驱动对象, 以下为创建不同浏览器驱动对象
    options = webdriver.ChromeOptions()
    options.add_experimental_option('detach', True) # 不自动关闭浏览
    driver = webdriver.Chrome(options=options)    # Chrome浏览器
    
    # driver = webdriver.Firefox()   # Firefox浏览器
    
    # driver = webdriver.Edge()      # Edge浏览器
    
    # driver = webdriver.Ie()        # Internet Explorer浏览器
    
    # driver = webdriver.Opera()     # Opera浏览器
    
    # driver = webdriver.PhantomJS()   # PhantomJS,无界面浏览器看上面 Chrome handless 文章
    
    # 打开指定网址
    driver.get('https://www.百度.com')
    
    # 休眠5秒
    sleep(5)
    
    # 关闭浏览器驱动对象
    driver.quit()
    

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanheakakb
系列文章
更多 icon
同类精品
更多 icon
继续加载