python - python selenium firefox - add_extension 不工作
问题描述
尝试将 uBlock 添加到浏览器会话,但它不起作用。
import selenium
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.options import Options as options
def establish_browser(type, hide):
browser = ''
if type == 'firefox':
ops = options()
ops.add_argument("--headless") if hide is True else ops.add_argument("--head")
profile = selenium.webdriver.FirefoxProfile()
profile.add_extension(extension='uBlock0@raymondhill.net.xpi')
browser = selenium.webdriver.Firefox(firefox_profile=profile, executable_path='geckodriver.exe', options=ops, firefox_binary=FirefoxBinary('C:/Program Files/Mozilla Firefox/firefox.exe'))
return browser
browser = establish_browser('firefox', False)
应该如何更改以使 uBlock 工作?
更新
chrome 版本似乎正在运行……</p>
if type == 'chrome':
from selenium.webdriver.chrome.options import Options as options
ops = options()
ops.add_argument("--headless") if hide is True else ops.add_argument("--head")
ops.add_extension("ublock.crx")
browser = selenium.webdriver.Chrome(executable_path='chromedriver.exe', options=ops, desired_capabilities={'binary_location': 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'})
Firefox 贬值了吗?
解决方案
因为,由于某种原因,chrome 的add_extension
作品,但 firefox 的add_extension
作品(目前)不起作用……这是我为 firefox 添加扩展的解决方法。
- 通过创建一个新的 Firefox 配置文件
right click windows start button > run > firefox.exe -P
- 然后添加你想要的任何扩展,ublock,adblock plus 等
- 调用您的个人资料文件夹
profile = selenium.webdriver.FirefoxProfile("C:/test")
browser = selenium.webdriver.Firefox(firefox_profile=profile, options=ops)
显然profile.add_extension()
不是此解决方法的必备条件
更新!- 添加了镀铬配置文件
出于对称目的,我更新了 chrome 示例代码以使用 chrome 配置文件,而不是.crx
直接调用。
- 将扩展安装到 chrome 的默认配置文件中。
导航到
C:\Users\User\AppData\Local\Google\Chrome
chromesUser Data
文件夹所在的位置。直接调用这个文件夹(绝对路径)或者重命名,调用相对路径。我已将其重命名为chrome_profile
:ops = options() ops.add_argument("--headless") if hide is True else ops.add_argument("--head") ops.add_argument('user-data-dir=chrome_profile') ops.add_argument('--profile-directory=Default') ops.add_argument("--incognito") browser = selenium.webdriver.Chrome(executable_path='chromedriver.exe', options=ops, desired_capabilities={'binary_location': 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'})
推荐阅读
- qt - 我想创建一个小工具来录制音频,在录制过程中,录制过程中有很多额外的噪音
- python-3.x - 无法在 read_csv pandas 上从 dtype('O') 转换数组
- c++ - 即使使用 lib 文件也无法修复未解析的外部符号 _AES_encrypt
- winforms - 从表单中删除控件:空间仍然被占用
- ios - 可以加快将崩溃日志分发到 Xcode 的速度吗?
- angular - Angular 5,动态表单,如何改变提交的方式和形式?
- python - 如何在延迟任务中执行 apply_async 任务
- ios - 如果 NSString 是类并且它是通过引用传递的,那么为什么是这个输出?
- mysql - Django 无法在 RHEL 7 中配置 MySQL
- php - Doctrine find 方法将 id 渲染为字符串