Search code examples
pythonseleniumfirefoxselenium-webdriverfirefox-marionette

How can I disable Web Driver Exceptions when using the Mozilla Marionette web driver with Selenium


I am remote controlling a Firefox browser using Python and Selenium. I have switched to using Marionette, as directed on the mozilla developer site. That all works fine.

There is one page, where when I want to select an element. I get an exception. I think it is a Javascript warning that is causing the driver to bork. Does anyone know how I can make the driver less picky about Javascript errors? Additionally does anyone know where there is comprehensive documentation of the Python Marionette client?

Sorry I can't make the code completely reproducible because it is a client's private site that I am attempting to select an element from.

from selenium import webdriver

# see https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities.FIREFOX

# Tell the Python bindings to use Marionette.
# This will not be necessary in the future,
# when Selenium will auto-detect what remote end
# it is talking to.
caps["marionette"] = True
caps["binary"] = "/Applications/Firefox.app/Contents/MacOS/firefox-bin"

from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
browser = webdriver.Firefox(capabilities=caps)
webdriver.Firefox.get_capabilities()
browser.implicitly_wait(3)

browser.get("https://www.example.com/examplepage")

saved_exports_field = browser.find_element_by_id('exportlist')
saved_exports_field_select = Select(saved_exports_field)

That is where it goes wrong. The trace is as follows

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-35-6e712759af43> in <module>()
      1 saved_exports_field = browser.find_element_by_id('exportlist')
----> 2 saved_exports_field_select = Select(saved_exports_field)
      3 #saved_exports_field_select.select_by_visible_text('test score export dan')

/Users/dan/anaconda/envs/lt/lib/python3.5/site-packages/selenium/webdriver/support/select.py in __init__(self, webelement)
     39                 webelement.tag_name)
     40         self._el = webelement
---> 41         multi = self._el.get_attribute("multiple")
     42         self.is_multiple = multi and multi != "false"
     43 

/Users/dan/anaconda/envs/lt/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py in get_attribute(self, name)
    134             attributeValue = self.parent.execute_script(
    135                 "return (%s).apply(null, arguments);" % raw,
--> 136                 self, name)
    137         else:
    138             resp = self._execute(Command.GET_ELEMENT_ATTRIBUTE, {'name': name})

/Users/dan/anaconda/envs/lt/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py in execute_script(self, script, *args)
    463         return self.execute(Command.EXECUTE_SCRIPT, {
    464             'script': script,
--> 465             'args': converted_args})['value']
    466 
    467     def execute_async_script(self, script, *args):

/Users/dan/anaconda/envs/lt/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    234         response = self.command_executor.execute(driver_command, params)
    235         if response:
--> 236             self.error_handler.check_response(response)
    237             response['value'] = self._unwrap_value(
    238                 response.get('value', None))

/Users/dan/anaconda/envs/lt/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    190         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    191             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 192         raise exception_class(message, screen, stacktrace)
    193 
    194     def _value_or_default(self, obj, key, default):

WebDriverException: Message: SyntaxError: missing ) in parenthetical

Thanks


Solution

  • There's a bug in release 3.0.0-beta-3 which prevents the use of get_attribute. So you can either revert to 3.0.0-beta-2 or you can fix the bug by editing the file yourself:

    In file /Users/dan/anaconda/envs/lt/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py, replace the line 133:

    raw = pkgutil.get_data(__package__, 'getAttribute.js')
    

    by:

    raw = pkgutil.get_data(__package__, 'getAttribute.js').decode('utf8')