Chr*_*ris 13 python firefox selenium firefox-profile
我正在尝试让 Python Selenium 在我的 Windows 机器上工作。我已升级到最新版本的 Firefox、Selenium、Geckodriver,但仍然收到以下错误:
Python脚本
from selenium import webdriver
driver = webdriver.Firefox()
Run Code Online (Sandbox Code Playgroud)
错误
Traceback (most recent call last):
File "run.py", line 17605, in <module>
File "<string>", line 21, in <module>
File "site-packages\selenium\webdriver\firefox\webdriver.py", line 77, in __init__
File "site-packages\selenium\webdriver\firefox\extension_connection.py", line 49, in __init__
File "site-packages\selenium\webdriver\firefox\firefox_binary.py", line 68, in launch_browser
File "site-packages\selenium\webdriver\firefox\firefox_binary.py", line 103, in _wait_until_connectable
WebDriverException: Message: Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details.
Run Code Online (Sandbox Code Playgroud)
我还尝试使用以下代码创建 firefox 配置文件:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))
profile.set_preference('general.warnOnAboutConfig', False)
gecko_path = "path_to_geckodriver\\geckodriver.exe"
path = "path_to_firefoxs\\Mozilla Firefox\\firefox.exe"
binary = FirefoxBinary(path)
driver = webdriver.Firefox(firefox_profile=profile,executable_path=gecko_path)
Run Code Online (Sandbox Code Playgroud)
Gab*_*ers 13
解决方案:
from selenium import webdriver
fp = webdriver.FirefoxProfile('/home/gabriel/.mozilla/firefox/whatever.selenium')
driver = webdriver.Firefox(fp)
Run Code Online (Sandbox Code Playgroud)
我一直在努力,直到我发现这个问题现在已经解决但很有帮助,因为它显示了一些命令。
第一个版本,无法正常工作,因为之后我无法与 selenium 连接:
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
options = Options()
options.add_argument("-profile")
options.add_argument("/home/gabriel/.mozilla/firefox/whatever.selenium")
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(capabilities=firefox_capabilities, firefox_options=options)
Run Code Online (Sandbox Code Playgroud)
我确信这会加载配置文件“whatever.selenium”,因为如果我转到 about:profiles 我可以阅读:
配置文件:selenium 这是正在使用的配置文件,无法删除。
即使“whatever.selenium”不是我系统上的默认配置文件。
备注:至少有一个配置文件参数被 selenium(或 geckodriver?)覆盖:首选项 > 隐私和安全 >“阻止弹出窗口”始终重置为关闭。因此,请使用 about:profiles 对您正在运行的配置文件进行断言。
笔记:
firefox_capabilities 在上面的代码中可能不需要。在 Windows 上我使用:
fp = webdriver.FirefoxProfile('C:/Users/x/AppData/Roaming/Mozilla/Firefox/Profiles/some-long-string')
driver = webdriver.Firefox(firefox_profile=fp)
...
Run Code Online (Sandbox Code Playgroud)
1 - 要查找当前的Profile Folder,请about:support在 url 字段中输入并按 Enter 键。
2 - 要查看所有用户配置文件,请about:profiles在 URL 字段中输入内容并按 Enter 键。
Jam*_*ott -1
切换到 chrome 驱动程序。Firefox、gecko 和 selenium 目前无法很好地协同工作。这最终对我有用。
import unittest
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
class TestTemplate(unittest.TestCase):
"""Include test cases on a given url"""
def setUp(self):
"""Start web driver"""
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.driver.implicitly_wait(10)
def tearDown(self):
"""Stop web driver"""
self.driver.quit()
def test_case_1(self):
"""Go to python.org and print title"""
try:
self.driver.get('https://www.python.org/')
title = self.driver.title
print title
except NoSuchElementException as ex:
self.fail(ex.msg)
def test_case_2(self):
"""Go to redbull.com and print title"""
try:
self.driver.get('https://www.redbull.com')
title = self.driver.title
print title
except NoSuchElementException as ex:
self.fail(ex.msg)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestTemplate)
unittest.TextTestRunner(verbosity=2).run(suite)
Run Code Online (Sandbox Code Playgroud)
我使用 Jenkinsfile 加载帧缓冲区来调用 selenium 和 python 脚本。
您可以轻松地在本地计算机上运行它。您可能想要一个与 Linux 一起使用的 vagrant box。
这是启动 python 脚本的内容。
sh "(Xvfb :99 -屏幕 0 1366x768x16 &) && (python ./${PYTHON_SCRIPT_FILE})"
这是从运行此 Docker 映像的 docker 文件调用的。
https://github.com/cloudbees/java-build-tools-dockerfile
| 归档时间: |
|
| 查看次数: |
25899 次 |
| 最近记录: |