我使用Python和Selenium遠程控制Firefox瀏覽器。我已轉向使用木偶,如mozilla developer site所指示的那樣。這一切都很好。如何在Selenium中使用Mozilla Marionette Web驅動程序時禁用Web驅動程序例外
有一個頁面,當我想選擇一個元素。我得到一個異常。我認爲這是一個Javascript警告,導致司機bork。有誰知道我能如何讓駕駛員對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
感謝
謝謝,它修復了通過例外失敗的代碼。我仍然有一個奇怪的問題,因爲「select_by_visible_text」實際上不會選擇一個選項。如果我改變文本,那麼它會失敗,但似乎驅動程序試圖告訴Firefox選擇元素沒有通過。我將不得不嘗試更多的調試。 – Dan
@丹,我也遇到了本地下拉列表中的一些問題。我不得不選擇
謝謝Florent,我可以試試。目前我只是轉而使用Chrome – Dan