WebDriverException:消息:使用safaridriver和Selenium 3.13.0显式等待时找不到命令'GET /session/7.../displayed'

时间:2018-07-26 15:12:41

标签: python selenium selenium-webdriver safaridriver selenium3

我正在使用如下所示的显式等待,以检查元素是否可点击。

WebDriverWait(driver, 30).until(
        expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "#search")))

但是我得到了错误

 <class 'selenium.common.exceptions.WebDriverException'>
Message: The command 'GET /session/.../displayed' was not found.

如果我使用time.sleep()可以正常工作,而不是显式等待。我已将safari驱动程序初始化为

from selenium.webdriver import Safari
driver = Safari()

这是stacktrace

  File "/Users/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/support/wait.py", line 71, in until
    value = method(self._driver)
  File "/Users/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/support/expected_conditions.py", line 283, in __call__
    element = visibility_of_element_located(self.locator)(driver)
  File "/Users/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/support/expected_conditions.py", line 127, in __call__
    return _element_if_visible(_find_element(driver, self.locator))
  File "/Users/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/support/expected_conditions.py", line 147, in _element_if_visible
    return element if element.is_displayed() == visibility else False
  File "/Users/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/remote/webelement.py", line 490, in is_displayed
    return self._execute(Command.IS_ELEMENT_DISPLAYED)['value']
  File "/Users/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "/Users/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
    self.error_handler.check_response(response)
  File "/Users/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: The command 'GET /session/7.../displayed' was not found.

1 个答案:

答案 0 :(得分:4)

此错误消息...

 <class 'selenium.common.exceptions.WebDriverException'>
Message: The command 'GET /session/.../displayed' was not found.

...表示向/session/{session id}/element/{element id}/displayed发送 GET 请求失败。


显示的元素

根据WebDriver W3C Editor's Draftelement displayed算法为 boolean 状态,其中true表示元素已显示,并且{ {1}}表示该元素未显示。要计算元素上的状态,请调用false。如果这样做不会产生错误,请从此函数调用中返回返回值。否则,返回带有错误代码 未知错误的错误。

此功能通常对URI模板为

的GET请求公开
Call(bot.dom.isShown, null, element)

分析

您已调用:

/session/{session id}/element/{element id}/displayed

WebDriverWait(driver, 30).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "#search"))) 类在内部调用element_to_be_clickable(),并定义为:

visibility_of_element_located()

现在,class element_to_be_clickable(object): """ An Expectation for checking an element is visible and enabled such that you can click it.""" def __init__(self, locator): self.locator = locator def __call__(self, driver): element = visibility_of_element_located(self.locator)(driver) if element and element.is_enabled(): return element else: return False 类返回visibility_of_element_located(),其定义为:

_element_if_visible(_find_element(driver, self.locator))

同样,class visibility_of_element_located(object): """ An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. locator - used to find the element returns the WebElement once it is located and visible """ def __init__(self, locator): self.locator = locator def __call__(self, driver): try: return _element_if_visible(_find_element(driver, self.locator)) except StaleElementReferenceException: return False 返回_element_if_visible(),其定义为:

element if element.is_displayed() == visibility else False

因此,由于def _element_if_visible(element, visibility=True): return element if element.is_displayed() == visibility else False 的{​​{1}}条件失败,因此您将错误视为 if()请求 element.is_displayed() == visibility 端点失败。

没有相关的 HTML 先前的步骤,很难猜测确切的原因,但可能是以下两种情况之一:


原因

NoSuchElementException 的原因可能是以下之一:

  • 您采用的定位器策略无法识别HTML DOM中的任何元素。
  • 您采用的定位器策略无法识别该元素,因为该元素不在浏览器的Viewport中。
  • 您采用的定位器策略可以识别元素,但由于存在属性 style =“ display:none;” 而不会显示。
  • 您采用的定位器策略不能唯一标识 HTML DOM 中所需的元素,并且当前可以找到其他隐藏 / 不可见元素。
  • 您要查找的 WebElement 位于GET标记内。

解决方案

解决 NoSuchElementException 的解决方案可以是以下任意一种: