How to load firefox profile with Python Selenium?

前端 未结 5 972
囚心锁ツ
囚心锁ツ 2021-02-14 20:19

I\'m trying to get Python Selenium to work on my Windows Machine. I\'ve upgraded to the latest versions of Firefox, Selenium, Geckodriver, but I still receive the below error: <

5条回答
  •  [愿得一人]
    2021-02-14 20:55

    Switch to the chrome driver. Firefox, gecko, and selenium are not working well together right now. Here is what finally worked for me.

    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)
    

    I use a Jenkinsfile that loads the frame buffer to call selenium and the python script.

    You could easily run this on your local machine. You may want to get a vagrant box going with linux.

    Here is what launches the python script.

    sh "(Xvfb :99 -screen 0 1366x768x16 &) && (python ./${PYTHON_SCRIPT_FILE})"

    This is called from a docker file running this Docker image.

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

提交回复
热议问题