如何在使用Selenium的Mozilla Marionette Web驱动程序时禁用Web驱动程序例外

时间:2016-09-16 09:20:14

标签: python selenium firefox selenium-webdriver firefox-marionette

我使用Python和Selenium远程控制Firefox浏览器。我按照mozilla developer site的指示切换到使用Marionette。一切正常。

有一页,我想选择一个元素。我得到一个例外。我认为这是一个Javascript警告,导致驱动程序bork。有谁知道如何让驱动程序对Javascript错误不那么挑剔?另外有谁知道哪里有Python Marionette客户端的全面文档?

抱歉,我无法使代码完全可重现,因为它是我试图从中选择元素的客户端私有网站。

ng2-select

这就是出错的地方。跟踪如下

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)

由于

2 个答案:

答案 0 :(得分:1)

版本3.0.0-beta-3中存在一个错误,阻止使用get_attribute。因此,您可以恢复到3.0.0-beta-2,也可以通过自己编辑文件来修复错误:

在档案中 /Users/dan/anaconda/envs/lt/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py,替换第133行:

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

由:

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

答案 1 :(得分:0)

首先回答你的第二个问题,this documentation似乎相当全面;这满足了你的需求吗?

至于如何禁用WebDriverException的问题,我唯一知道的就是使用try: except:块,但我不认为这会是一个好主意。 WebDriverException是webdriver使用的基本异常,它将捕获您正在使用的所有错误,包括NoSuchElementException

我不知道有任何方法专门捕获JavaScript错误,因为它们似乎冒出了WebDriverException s。我假设因为你问这个问题,修复JavaScript本身不是一个选择吗?

您可能尝试的一件事是使用网络驱动程序get_log() method。根据我的阅读,JS错误应该在此方法返回的结果中可见。您可以尝试拨打browser.get_log(log_type)(其中log_type'browser''client''driver''server'中的一个,具体取决于错误发生的位置)在Select()调用之前,解析该数据,然后采取相应的行动。