gpt4 book ai didi

python - WebDriverException : Message: The command 'GET/session/7.../displayed' was not found while Explicit Wait with safaridriver and Selenium 3. 13.0

转载 作者:太空狗 更新时间:2023-10-29 17:20:32 26 4
gpt4 key购买 nike

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

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() 它工作正常而不是 explicir wait 它工作正常。我已将 safari 驱动程序初始化为

from selenium.webdriver import Safari
driver = Safari()

这是堆栈跟踪

  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.

最佳答案

这个错误信息...

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

...暗示GET请求/session/{session id}/element/{element id}/displayed失败了。


显示的元素

根据 WebDriver W3C Editor's Draft element displayed算法是一个 bool 状态,其中true表示该元素被显示并且false表示该元素未显示。要计算元素的状态,请调用 Call(bot.dom.isShown, null, element) .如果这样做没有产生错误,则返回此函数调用的返回值。否则返回带有错误代码 未知错误的错误。

此函数通常暴露给 URI 模板为

的 GET 请求
/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()类(class)返回_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

因此,作为 if() element.is_displayed() == visibility 的状况失败 因此您看到错误为 GET请求 /session/{session id}/element/{element id}/displayed 端点失败。

如果没有相关的 HTML前面的步骤,很难猜测确切的原因,但可能是以下任一原因:


原因

NoSuchElementException 的原因可能是以下任一情况:

  • 您采用的定位器策略没有识别 HTML DOM 中的任何元素.
  • 您采用的定位器策略无法识别该元素,因为它不在浏览器的 Viewport 中。 .
  • 您采用的定位器策略标识元素,但由于属性style="display: none;" 的存在而不可见。
  • 您采用的定位器策略不能唯一识别HTML DOM中的所需元素,目前可以找到其他一些隐藏的/不可见的元素。
  • 您要查找的 WebElement<iframe> 中标签。

解决方案

解决 NoSuchElementException 的方法可以是以下之一:

关于python - WebDriverException : Message: The command 'GET/session/7.../displayed' was not found while Explicit Wait with safaridriver and Selenium 3. 13.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51542203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com