python+selenium下载文件,firefox_profile方法弃用解决

在学习使用WebDriver下载文件时,遇到了警告“DeprecationWarning: firefox_profile has been deprecated, please use an Options object ”

这句话的意思就是“弃用警告:firefox\u配置文件已弃用,请使用选项对象”

   虽然这样目前不影响使用,但我们可以通过改为使用FirefoxOptions()方法消除警告,代码如下

from selenium import webdriver
from selenium.webdriver.common.by import By
import os

fp = webdriver.FirefoxOptions()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.dir",os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","binary//octet-stream")

driver = webdriver.Firefox(options=fp)
driver.get("https://pypi.org/project/selenium/#files")
driver.find_element(By.PARTIAL_LINK_TEXT,"selenium-4.1.0-py3-none-any.whl").click()

  现在再运行就没有警告啦~

 

要在Python3和Selenium4中设置统一的配置文件路径,可以使用以下代码: ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument("--user-data-dir=/path/to/profile") driver = webdriver.Chrome(options=chrome_options) ``` 在这个例子中,我们创建了一个名为`chrome_options`的选项对象,并使用`--user-data-dir`参数来指定配置文件的路径。你需要将`/path/to/profile`替换为你自己的配置文件路径。 通过这种方式,当你启动Chrome浏览器时,它将读取指定路径下的配置文件,包括书签、历史记录、插件、扩展等信息。这使得你可以轻松地在不同的脚本中共享配置文件,而不必每次都重新配置。 如果你使用的是Firefox浏览器,可以使用类似的方法: ```python from selenium import webdriver from selenium.webdriver.firefox.options import Options firefox_options = Options() firefox_options.set_preference("browser.profile", "/path/to/profile") driver = webdriver.Firefox(options=firefox_options) ``` 在这个例子中,我们创建了一个名为`firefox_options`的选项对象,并使用`browser.profile`首选项来指定配置文件的路径。你需要将`/path/to/profile`替换为你自己的配置文件路径。 无论你使用的是Chrome还是Firefox,使用统一的配置文件路径可以帮助你更方便地管理浏览器配置和数据,从而提高你的工作效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值