我正在尝试让 Python Selenium 在我的 Windows 机器上工作。我已经升级到最新版本的 Firefox、Selenium、Geckodriver,但仍然收到以下错误:
Python 脚本
from selenium import webdriver
driver = webdriver.Firefox()
错误
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.
我还尝试使用以下代码创建 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)
解决方案:
from selenium import webdriver
fp = webdriver.FirefoxProfile('/home/gabriel/.mozilla/firefox/whatever.selenium')
driver = webdriver.Firefox(fp)
我一直在努力,直到发现这个问题,现在已经解决了,但很有帮助,因为它显示了一些命令。
第一个版本,无法工作,因为之后我无法连接到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)
我确信这会加载配置文件“whatever.selenium”,因为如果我转到 about:profiles 我可以阅读:
简介:硒 这是正在使用的配置文件,无法删除。
尽管“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)
...
1 - 要查找当前的
Profile Folder
,请在 url 字段中输入 about:support
,然后按 Enter 键。about:profiles
,然后按 Enter。
您没有更新个人资料的首选项:profile.update_preferences()。 设置首选项后,您必须更新配置文件。请按照以下代码操作:
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)
profile.update_preferences()
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)
公认的答案在现代对我不起作用。这样做
options = webdriver.FirefoxOptions()
options.add_argument("-profile")
options.add_argument('/home/brad/.mozilla/firefox/aoeu.myprofile')
driver = webdriver.Firefox(options=options)
切换到 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)
我使用 Jenkinsfile 加载帧缓冲区来调用 selenium 和 python 脚本。
您可以轻松地在本地计算机上运行它。您可能想要一个与 linux 一起使用的 vagrant box。
这是启动 python 脚本的内容。
sh "(Xvfb :99 -screen 0 1366x768x16 &) && (python ./${PYTHON_SCRIPT_FILE})"
这是从运行此 Docker 映像的 docker 文件调用的。
在java中加载自定义的firefox配置文件:
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile"); // manualy created profile in firefox profile manager
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
driver = new FirefoxDriver(options);
driver.manage().window().maximize();