Selenium学习(24)exceptions

selenium通用接口common中包含所有见接口错误,具体见:

selenium.common.exceptions – Selenium 3.13 documentation

以下说明常用接口错误以及分析

  1. WebDriverException

Base webdriver exception.

驱动异常。一般是我们第一个遇到的异常。可能的问题点有:

  • 驱动是否正确配置环境变量;
  • selenium工具版本、驱动版本与浏览器版本之间是否兼容;

2. ElementNotVisibleException

Thrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with.

Most commonly encountered when trying to click or read text of an element that is hidden from view.

元素不可见。一般发生在定位隐藏元素时,定位方式不对,可能的问题点有:

  • 元素是动态加载的,采用的定位方式不合适,第一次正常,第二次却失败;
  • iframe导致不能定位;
  • 元素默认隐藏,例如下拉列表等,需要逐步定位;

3. ElementNotSelectableException

Thrown when trying to select an unselectable element.

For example, selecting a ‘script’ element.

元素不能被选择。虽然元素在DOM中,但是disable状态,不能被点击或选择,例如文字描述等等。

4. InvalidSelectorException

selenium.common.exceptions.InvalidSelectorException:Thrown when the selector which is used to find an element does not return a WebElement. Currently this only happens when the selector is an xpath expression and it is either syntactically invalid (i.e. it is not a xpath expression) or the expression does not select WebElements (e.g. “count(//input)”).

定位信息错误。一般发生在写错了定位信息。例如:通过name进行定位,但是内容写错了。

5. NoSuchElementException

Thrown when element could not be found.

If you encounter this exception, you may want to check the following:

  • Check your selector used in your find_by…
  • Element may not yet be on the screen at the time of the find operation, (webpage is still loading) see selenium.webdriver.support.wait.WebDriverWait() for how to write a wait wrapper to wait for an element to appear.

6. NoSuchFrameException

Thrown when frame target to be switched doesn’t exist.

表单不存在。

7. NoAlertPresentException

Thrown when switching to no presented alert.

This can be caused by calling an operation on the Alert() class when an alert is not yet on the screen.

告警页面不存在。一般是因为页面刷新较慢,需要加延时。

8. NoSuchWindowException

Thrown when window target to be switched doesn’t exist.

To find the current set of active window handles, you can get a list of the active window handles in the following way:

print(driver.window_handles)

页面不存在。一般是因为页面刷新较慢,需要加延时。

9. StaleElementReferenceException

Thrown when a reference to an element is now “stale”.

Stale means the element no longer appears on the DOM of the page.

Possible causes of StaleElementReferenceException include, but not limited to:

  • You are no longer on the same page, or the page may have refreshed since the element was located.
  • The element may have been removed and re-added to the screen, since it was located. Such as an element being relocated. This can happen typically with a javascript framework when values are updated and the node is rebuilt.
  • Element may have been inside an iframe or another context which was refreshed.

The referenced element is no longer present on the DOM page (reference to an element is now Stale). E.g. The Element belongs to a different frame than the current one OR the user has navigated away to another page.

10. TimeoutException

Thrown when a command does not complete in enough time.

E.g. the element didn’t display in the specified time. Encountered when working with waits.

参考:

11 Most common Exceptions in Selenium WebDriver

selenium.common.exceptions – Selenium 3.13 documentation

    原文作者:Python初学者
    原文地址: https://zhuanlan.zhihu.com/p/41658426
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞