4

Trying to run simple code in ubuntu 18.04 to get selenium to start a Firefox webdriver.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
import traceback

options = Options()
options.headless = True

try:
    print('Trying to create the driver')
    driver = webdriver.Firefox(options=options)
except Exception as e:
    tb = traceback.format_exc()
else:
    try:
        print('Trying to access ubuntu.com')
        driver.get('http://www.ubuntu.com/')
    except Exception as e:
        print(e)
    else:
        time.sleep(10)
        try:
            print('Trying to close the driver')
            driver.close()
            print('Task done')
        except Exception as e:
            print(e)
finally:
    print(tb)

When I run the code as root here is the traceback:

Trying to create the driver
Traceback (most recent call last):
  File "test_sel.py", line 11, in <module>
    driver = webdriver.Firefox(options=options)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: connection refused

The output from the geckodriver.log file from the last run of the code is:

1580849922419mozrunner::runnerINFORunning command: "/usr/bin/firefox" "-marionette" "-headless" "-foreground" "-no-remote
" "-profile" "/tmp/rust_mozprofileHprun6"
*** You are running in headless mode.
1580849923532addons.webextension.doh-rollout@mozilla.orgWARNLoading extension 'doh-rollout@mozilla.org': Reading manifest: 
Invalid extension permission: networkStatus
1580849923888addons.webextension.screenshots@mozilla.orgWARNLoading extension 'screenshots@mozilla.org': Reading manifest: 
Invalid extension permission: mozillaAddons
1580849923891addons.webextension.screenshots@mozilla.orgWARNLoading extension 'screenshots@mozilla.org': Reading manifest: 
Invalid extension permission: telemetry
1580849923892addons.webextension.screenshots@mozilla.orgWARNLoading extension 'screenshots@mozilla.org': Reading manifest: 
Invalid extension permission: resource://pdf.js/
1580849923892addons.webextension.screenshots@mozilla.orgWARNLoading extension 'screenshots@mozilla.org': Reading manifest: 
Invalid extension permission: about:reader*

My selenium version is 3.14, my geckodriver version is 0.26, my Firefox version is >70, and according to specs I shouldn't see any compatibility issues. geckodriver file is in both the '/usr/local/bin' and my working folders. geckodriver.log and geckodriver both have 777 permissions.

Not sure what to do at this point. Please help.

When the code is ran as non-root the stack trace is:

Trying to create the driver
Traceback (most recent call last):
  File "test_sel.py", line 11, in <module>
    driver = webdriver.Firefox(options=options)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: connection refused

And the geckodriver.log says:

1580851603716mozrunner::runnerINFORunning command: "/usr/bin/firefox" "-marionette" "-headless" "-foreground" "-no-remote" 
"-profile" "/tmp/rust_mozprofileCP9aHJ"
*** You are running in headless mode.
1580851605231addons.webextension.doh-rollout@mozilla.orgWARNLoading extension 'doh-rollout@mozilla.org': Reading manifest: In
valid extension permission: networkStatus
1580851605513addons.webextension.screenshots@mozilla.orgWARNLoading extension 'screenshots@mozilla.org': Reading manifest: In
valid extension permission: mozillaAddons
1580851605513addons.webextension.screenshots@mozilla.orgWARNLoading extension 'screenshots@mozilla.org': Reading manifest: In
valid extension permission: telemetry
1580851605513addons.webextension.screenshots@mozilla.orgWARNLoading extension 'screenshots@mozilla.org': Reading manifest: In
valid extension permission: resource://pdf.js/
1580851605513addons.webextension.screenshots@mozilla.orgWARNLoading extension 'screenshots@mozilla.org': Reading manifest: In
valid extension permission: about:reader*
JavaScript error: resource://gre/modules/Prompter.jsm, line 437: NS_ERROR_NOT_AVAILABLE: Cannot call openModalWindow on a hidden window
midav
  • 91
  • 2
  • 7
  • Update the question with the complete error stack trace. – undetected Selenium Feb 04 '20 at 20:31
  • @DebanjanB just added – midav Feb 04 '20 at 20:50
  • Please don't handcraft the error and error stack trace, publish the complete error. – undetected Selenium Feb 04 '20 at 21:11
  • @DebanjanB I'm not sure what you mean. This is the traceback I get when I add in the traceback module, which is the same error without the traceback module. – midav Feb 04 '20 at 21:16
  • Okay, can you execute your tests as a **non-root** user and update me the status please? – undetected Selenium Feb 04 '20 at 21:19
  • @DebanjanB thank you. Please see the addition to my question that I posted. – midav Feb 04 '20 at 21:32
  • Checkout the points mentioned in the duplicate targets. If the issue still persists update the question with your modified code and error, I will address your concerns. – undetected Selenium Feb 04 '20 at 21:41
  • @DebanjanB Thank you for your help. I looked at both of the suggested questions and unfortunately none of them help me. As I mentioned I updated my Firefox, selenium, and geckodriver and ensured they can be ran by non-root users. – midav Feb 04 '20 at 23:05
  • Can you update the question what exactly you have tried? – undetected Selenium Feb 04 '20 at 23:10
  • 2
    @DebanjanB thank you. I found what I think is the problem. I work on AWS and I created a new ubuntu server to see if the code would run. I put in python commands line-by-line and created a webdriver instance but then did not close the webdriver at the end and it appears while one port was open I couldn't start another driver instance. I rebooted my server, ran the python commands again, closed the webdriver at the end, and was able to instantiate another webdriver session later. I believe it is important for others to see my solution and so I ask if you could allow me to post my answer. – midav Feb 05 '20 at 17:46
  • I had a compute engine in Google Cloud. After resetting this VM, it works. I believe this is the same solution @midav gave and is the correct answer in my case. – Dan Cogswell Oct 27 '21 at 18:45

0 Answers0