我正在使用 Python 和 Selenium 远程控制 Firefox 浏览器。按照mozilla 开发者网站上的指示,我已改用 Marionette 。这一切都很好。
有一页,当我想选择一个元素时。我得到一个例外。我认为这是导致驱动程序崩溃的 Javascript 警告。有谁知道如何让驱动程序对 Javascript 错误不那么挑剔?此外,有谁知道 Python Marionette 客户端的综合文档在哪里?
抱歉,我无法使代码完全可重现,因为它是我试图从中选择元素的客户私人网站。
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)
这就是它出错的地方。轨迹如下
---------------------------------------------------------------------------
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
谢谢