zoukankan      html  css  js  c++  java
  • python + selenium + firefox 自定义配置文件启动浏览器

    网上资源参差不齐,找了很多文件,直接上代码,后续深究继续补充,上代码:

     1     def gen_driver(self) -> Firefox:
     2         """
     3         生成一个driver
     4         generate a driver
     5         :return: webdriver
     6         """
     7         # 火狐配置文件,
     8         # 通过加载火狐配置文件,实现免登陆访问网站(就是已经在浏览器中保存好登陆信息如cookies之类的,再次访问就不用重新登录)
     9         # 创建一个FirefoxProfile实例
    10         profile = FirefoxProfile()
    11         # selenium firefox设置代理(默认是0,就是直接连接;1就是手工配置代理)
    12         profile.set_preference('network.proxy.type', 0)
    13         # 指定下载路径
    14         profile.set_preference('browser.download.dir', self.excel_file_dir)
    15         # 设置成 2 表示使用自定义下载路径;设置成 0 表示下载到桌面;设置成 1 表示下载到默认路径
    16         profile.set_preference('browser.download.folderList', 2)
    17         # 在开始下载时是否显示下载管理器
    18         profile.set_preference('browser.download.manager.showWhenStarting', False)
    19         # 设置正确的文件的Content_Type
    20         profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/octet-stream')
    21         # 设置浏览器语言
    22         profile.set_preference("intl.accept_languages", "zh-CN")
    23 
    24         # 驱动选项
    25         options = FirefoxOptions()
    26         # 参数为HEADLESS时,浏览器为无头模式
    27         if self.view == HEADLESS:
    28             options.add_argument('--headless')
    29 
    30         # 以代理方式方式启动firefox(配置文件,选项参数)
    31         driver = Firefox(
    32                 firefox_profile=profile, options=options,
    33         )
    34         # 隐式等待
    35         driver.implicitly_wait(10)
    36         self.wait = WebDriverWait(driver, 10, 0.1)
    37 
    38         return driver

    有兴趣的博友,可以找一找专门讲解selenium模块的书籍

    自有风云来时雨, 似有风霜沾蓑衣
  • 相关阅读:
    MySQL中去重字段完全相同的数据
    SVN自动更新-win平台
    EF出错:Unable to convert MySQL date/time value to System.DateTime
    微信不支持Object.assign
    记录一下dotnetcore.1.0.0-VS2015Tools.preview2安装不上的问题
    Ajax表单异步上传(包括文件域)
    .NET web开发之WebApi初试水
    遍历数组一次求得数组的平均数、标准差、方差
    记STM32F030多通道ADC DMA读取乱序问题
    RT-Thread入门和模拟器的配置生成
  • 原文地址:https://www.cnblogs.com/meipu/p/12880532.html
Copyright © 2011-2022 走看看