Python Selenium在ubuntu中设置firefox配置文件的路径

标签 python selenium ubuntu selenium-webdriver firefox

我已经使用 python 和 Selenium 在 Ubuntu 中设置了新创建的 Firefox 配置文件的路径。但是当我运行 python 脚本时,我遇到了这个问题

/bin/python3 /home/frixreda/Desktop/Python/testU.py
/home/frixreda/Desktop/Python/testU.py:7: DeprecationWarning: firefox_profile has been deprecated, please use an Options object
  profile = webdriver.FirefoxProfile(
/home/frixreda/Desktop/Python/testU.py:13: DeprecationWarning: capabilities and desired_capabilities have been deprecated, please pass in a Service object
  driver = webdriver.Firefox(firefox_profile=profile,
/home/frixreda/Desktop/Python/testU.py:13: DeprecationWarning: firefox_profile has been deprecated, please pass in an Options object
  driver = webdriver.Firefox(firefox_profile=profile,
这是我的python脚本:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

profile = webdriver.FirefoxProfile(
       r'/home/frixreda/.mozilla/firefox/3uz1obam.default')
profile.set_preference("dom.webdriver.enabled", False)
profile.set_preference('useAutomationExtension', False)
profile.update_preferences()
desired = DesiredCapabilities.FIREFOX
driver = webdriver.Firefox(firefox_profile=profile,
                     desired_capabilities=desired)

driver.get("https://gmail.com/")

最佳答案

Browsers正在改变和Selenium还更改了一些设置 - 现在不推荐使用旧方法但仍然可以工作(这只是警告,而不是错误)。
但警告显示首选方法是使用 Option()而不是 FirefoxProfile()并且不需要使用DesiredCapabilities.FIREFOX这增加了

{'browserName': 'firefox', 'marionette': True, 'acceptInsecureCerts': True}
因为它自动在 options.capabilities
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
#from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = Options()

#help(options)

options.add_argument('-profile')
options.add_argument('/home/frixreda/.mozilla/firefox/3uz1obam.default')

options.set_preference('dom.webdriver.enabled', False)
options.set_preference('useAutomationExtension', False)

#options.set_headless()  # deprecated
#options.headless = True # preferred

#options.set_capability(name, value)
#print("DesiredCapabilities:", DesiredCapabilities.FIREFOX)  # {'browserName': 'firefox', 'marionette': True, 'acceptInsecureCerts': True}

print('accept_insecure_certs:', options.accept_insecure_certs)
print('arguments   :', options.arguments)
print('preferences :', options.preferences)
print('capabilities:', options.capabilities)
print('headless    :', options.headless)
print('profile     :', options.profile)  # `None`??? but browser uses profile
print('proxy       :', options.proxy)
print('binary      :', options.binary)
#print('binary_location:', options.binary_location)
print('log         :', options.log)

driver = webdriver.Firefox(options=options)

#driver.get("https://stackoverflow.com")
driver.get("https://gmail.com")

顺便说一句:您可以使用 help(options)查看所有带有说明的选项。

关于Python Selenium在ubuntu中设置firefox配置文件的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70272235/

相关文章:

python - mysql查询在mysql-python中得到不同的结果

Python 3.4 缺少 _ssl.pyd

python - ModuleNotFoundError : No module named 'webdriver_manager' error even after installing webdrivermanager

linux - Oracle 12c Ubuntu 17.04 安装报错

python - 查找 Pandas 移动平均线当前和之前交叉点之间的最小值

python - 使用 Python 进行网页抓取 : How to scroll into a view by pixels?

angularjs - 使用来自浏览器的信息对多个页面进行 E2E 测试

ruby - 在 Ubuntu 中安装 Ruby 1.9.3 出现错误

linux - 如何生成 fullchain.pem 和 privkey.pem?

python - 设置 matplotlib colorbar 范围(比绘制的值范围更大)