首页 文章

如何使用Python Selenium加载firefox配置文件?

提问于
浏览
0

我正在尝试让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)
  • Python 2.7

  • Firefox 60

  • Geckodriver-v0.20.1-win64.zip

  • Selenium 3.12.0

2 回答

  • 0

    切换到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)
    

    我使用Jenkins文件加载帧缓冲区来调用selenium和python脚本 .

    您可以在本地计算机上轻松运行它 . 你可能想要一个流浪汉盒子与linux .

    这是启动python脚本的原因 .

    sh“(Xvfb:99 -screen 0 1366x768x16&)&&(python ./$)”

    这是从运行此Docker镜像的docker文件调用的 .

    https://github.com/cloudbees/java-build-tools-dockerfile

  • 0

    在java中加载自定义的firefox配置文件:

    FirefoxOptions options = new FirefoxOptions();
    
    ProfilesIni allProfiles = new ProfilesIni();         
    FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
    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();
    

相关问题