我正在使用如下所示的显式等待,以检查元素是否可点击。
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.
答案 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 Draft,element 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 的原因可能是以下之一:
GET
标记内。解决 NoSuchElementException 的解决方案可以是以下任意一种:
采用一个Locator Strategy来唯一标识所需的 WebElement 。您可以借助开发人员工具( Ctrl + Shift + I 或 F12 ),然后使用元素检查器。
在这里您将找到有关how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?
使用execute_script()
方法滚动查看元素,如下所示:
/session/{session id}/element/{element id}/displayed
在这里您将找到有关Scrolling to top of the page in Python using Selenium
如果元素具有属性 style =“ display:none;” ,请通过<iframe>
方法删除该属性,如下所示:
elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].scrollIntoView();", elem)
要检查元素是否在executeScript()
内,遍历 HTML 以找到相应的elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].removeAttribute('style')", elem)
elem.send_keys("text_to_send")
标签和<iframe>
所需的 iframe 通过以下两种方法之一:
<iframe>
在这里您可以找到有关How can I select a html element no matter what frame it is in in selenium?的详细讨论。