Selenium是一个用于Web应用程序自动化测试工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。

1.目录结构如下:

D:\soft\python36\Lib\site-packages\selenium>tree /f
卷 新加卷 的文件夹 PATH 列表
卷序列号为 960E-961E
D:.
│ __init__.py

├─common
│ │ exceptions.py
│ │ __init__.py
│ │
│ └─__pycache__
│ exceptions.cpython-36.pyc
│ __init__.cpython-36.pyc

├─webdriver
│ │ __init__.py
│ │
│ ├─android
│ │ │ webdriver.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ webdriver.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─blackberry
│ │ │ webdriver.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ webdriver.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─chrome
│ │ │ options.py
│ │ │ remote_connection.py
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ options.cpython-36.pyc
│ │ remote_connection.cpython-36.pyc
│ │ service.cpython-36.pyc
│ │ webdriver.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─common
│ │ │ action_chains.py
│ │ │ alert.py
│ │ │ by.py
│ │ │ desired_capabilities.py
│ │ │ keys.py
│ │ │ proxy.py
│ │ │ service.py
│ │ │ touch_actions.py
│ │ │ utils.py
│ │ │ __init__.py
│ │ │
│ │ ├─actions
│ │ │ │ action_builder.py
│ │ │ │ input_device.py
│ │ │ │ interaction.py
│ │ │ │ key_actions.py
│ │ │ │ key_input.py
│ │ │ │ mouse_button.py
│ │ │ │ pointer_actions.py
│ │ │ │ pointer_input.py
│ │ │ │ __init__.py
│ │ │ │
│ │ │ └─__pycache__
│ │ │ action_builder.cpython-36.pyc
│ │ │ input_device.cpython-36.pyc
│ │ │ interaction.cpython-36.pyc
│ │ │ key_actions.cpython-36.pyc
│ │ │ key_input.cpython-36.pyc
│ │ │ mouse_button.cpython-36.pyc
│ │ │ pointer_actions.cpython-36.pyc
│ │ │ pointer_input.cpython-36.pyc
│ │ │ __init__.cpython-36.pyc
│ │ │
│ │ ├─html5
│ │ │ │ application_cache.py
│ │ │ │ __init__.py
│ │ │ │
│ │ │ └─__pycache__
│ │ │ application_cache.cpython-36.pyc
│ │ │ __init__.cpython-36.pyc
│ │ │
│ │ └─__pycache__
│ │ action_chains.cpython-36.pyc
│ │ alert.cpython-36.pyc
│ │ by.cpython-36.pyc
│ │ desired_capabilities.cpython-36.pyc
│ │ keys.cpython-36.pyc
│ │ proxy.cpython-36.pyc
│ │ service.cpython-36.pyc
│ │ touch_actions.cpython-36.pyc
│ │ utils.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─edge
│ │ │ options.py
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ options.cpython-36.pyc
│ │ service.cpython-36.pyc
│ │ webdriver.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─firefox
│ │ │ extension_connection.py
│ │ │ firefox_binary.py
│ │ │ firefox_profile.py
│ │ │ options.py
│ │ │ remote_connection.py
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ webdriver.xpi
│ │ │ webdriver_prefs.json
│ │ │ webelement.py
│ │ │ __init__.py
│ │ │
│ │ ├─amd64
│ │ │ x_ignore_nofocus.so
│ │ │
│ │ ├─x86
│ │ │ x_ignore_nofocus.so
│ │ │
│ │ └─__pycache__
│ │ extension_connection.cpython-36.pyc
│ │ firefox_binary.cpython-36.pyc
│ │ firefox_profile.cpython-36.pyc
│ │ options.cpython-36.pyc
│ │ remote_connection.cpython-36.pyc
│ │ service.cpython-36.pyc
│ │ webdriver.cpython-36.pyc
│ │ webelement.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─ie
│ │ │ options.py
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ options.cpython-36.pyc
│ │ service.cpython-36.pyc
│ │ webdriver.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─opera
│ │ │ options.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ options.cpython-36.pyc
│ │ webdriver.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─phantomjs
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ service.cpython-36.pyc
│ │ webdriver.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─remote
│ │ │ command.py
│ │ │ errorhandler.py
│ │ │ file_detector.py
│ │ │ getAttribute.js
│ │ │ isDisplayed.js
│ │ │ mobile.py
│ │ │ remote_connection.py
│ │ │ switch_to.py
│ │ │ utils.py
│ │ │ webdriver.py
│ │ │ webelement.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ command.cpython-36.pyc
│ │ errorhandler.cpython-36.pyc
│ │ file_detector.cpython-36.pyc
│ │ mobile.cpython-36.pyc
│ │ remote_connection.cpython-36.pyc
│ │ switch_to.cpython-36.pyc
│ │ utils.cpython-36.pyc
│ │ webdriver.cpython-36.pyc
│ │ webelement.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─safari
│ │ │ permissions.py
│ │ │ remote_connection.py
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ permissions.cpython-36.pyc
│ │ remote_connection.cpython-36.pyc
│ │ service.cpython-36.pyc
│ │ webdriver.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─support
│ │ │ abstract_event_listener.py
│ │ │ color.py
│ │ │ events.py
│ │ │ event_firing_webdriver.py
│ │ │ expected_conditions.py
│ │ │ select.py
│ │ │ ui.py
│ │ │ wait.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ abstract_event_listener.cpython-36.pyc
│ │ color.cpython-36.pyc
│ │ events.cpython-36.pyc
│ │ event_firing_webdriver.cpython-36.pyc
│ │ expected_conditions.cpython-36.pyc
│ │ select.cpython-36.pyc
│ │ ui.cpython-36.pyc
│ │ wait.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ ├─webkitgtk
│ │ │ options.py
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ options.cpython-36.pyc
│ │ service.cpython-36.pyc
│ │ webdriver.cpython-36.pyc
│ │ __init__.cpython-36.pyc
│ │
│ └─__pycache__
│ __init__.cpython-36.pyc

└─__pycache__
__init__.cpython-36.pyc

selenium目录结构

2.捕获异常可调用exceptions.py使用其中的相应方法

# 需要导入模块exceptions.py
from selenium.common import exceptions # exceptions.py代码如下
class WebDriverException(Exception):
"""
Base webdriver exception.
""" def __init__(self, msg=None, screen=None, stacktrace=None):
self.msg = msg
self.screen = screen
self.stacktrace = stacktrace def __str__(self):
exception_msg = "Message: %s\n" % self.msg
if self.screen is not None:
exception_msg += "Screenshot: available via screen\n"
if self.stacktrace is not None:
stacktrace = "\n".join(self.stacktrace)
exception_msg += "Stacktrace:\n%s" % stacktrace
return exception_msg class ErrorInResponseException(WebDriverException):
"""
Thrown when an error has occurred on the server side. This may happen when communicating with the firefox extension
or the remote driver server.
"""
def __init__(self, response, msg):
WebDriverException.__init__(self, msg)
self.response = response class InvalidSwitchToTargetException(WebDriverException):
"""
Thrown when frame or window target to be switched doesn't exist.
"""
pass class NoSuchFrameException(InvalidSwitchToTargetException):
"""
Thrown when frame target to be switched doesn't exist.
"""
pass class NoSuchWindowException(InvalidSwitchToTargetException):
"""
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 """
pass class NoSuchElementException(WebDriverException):
"""
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.
"""
pass class NoSuchAttributeException(WebDriverException):
"""
Thrown when the attribute of element could not be found. You may want to check if the attribute exists in the particular browser you are
testing against. Some browsers may have different property names for the same
property. (IE8's .innerText vs. Firefox .textContent)
"""
pass class StaleElementReferenceException(WebDriverException):
"""
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.
"""
pass class InvalidElementStateException(WebDriverException):
"""
Thrown when a command could not be completed because the element is in an invalid state. This can be caused by attempting to clear an element that isn't both editable and resettable.
"""
pass class UnexpectedAlertPresentException(WebDriverException):
"""
Thrown when an unexpected alert is appeared. Usually raised when when an expected modal is blocking webdriver form executing any
more commands.
"""
def __init__(self, msg=None, screen=None, stacktrace=None, alert_text=None):
super(UnexpectedAlertPresentException, self).__init__(msg, screen, stacktrace)
self.alert_text = alert_text def __str__(self):
return "Alert Text: %s\n%s" % (self.alert_text, super(UnexpectedAlertPresentException, self).__str__()) class NoAlertPresentException(WebDriverException):
"""
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.
"""
pass class ElementNotVisibleException(InvalidElementStateException):
"""
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.
"""
pass class ElementNotInteractableException(InvalidElementStateException):
"""
Thrown when an element is present in the DOM but interactions
with that element will hit another element do to paint order
"""
pass class ElementNotSelectableException(InvalidElementStateException):
"""
Thrown when trying to select an unselectable element. For example, selecting a 'script' element.
"""
pass class InvalidCookieDomainException(WebDriverException):
"""
Thrown when attempting to add a cookie under a different domain
than the current URL.
"""
pass class UnableToSetCookieException(WebDriverException):
"""
Thrown when a driver fails to set a cookie.
"""
pass class RemoteDriverServerException(WebDriverException):
"""
"""
pass class TimeoutException(WebDriverException):
"""
Thrown when a command does not complete in enough time.
"""
pass class MoveTargetOutOfBoundsException(WebDriverException):
"""
Thrown when the target provided to the `ActionsChains` move()
method is invalid, i.e. out of document.
"""
pass class UnexpectedTagNameException(WebDriverException):
"""
Thrown when a support class did not get an expected web element.
"""
pass class InvalidSelectorException(NoSuchElementException):
"""
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)").
"""
pass class ImeNotAvailableException(WebDriverException):
"""
Thrown when IME support is not available. This exception is thrown for every IME-related
method call if IME support is not available on the machine.
"""
pass class ImeActivationFailedException(WebDriverException):
"""
Thrown when activating an IME engine has failed.
"""
pass class InvalidArgumentException(WebDriverException):
"""
The arguments passed to a command are either invalid or malformed.
"""
pass class JavascriptException(WebDriverException):
"""
An error occurred while executing JavaScript supplied by the user.
"""
pass class NoSuchCookieException(WebDriverException):
"""
No cookie matching the given path name was found amongst the associated cookies of the
current browsing context's active document.
"""
pass class ScreenshotException(WebDriverException):
"""
A screen capture was made impossible.
"""
pass class ElementClickInterceptedException(WebDriverException):
"""
The Element Click command could not be completed because the element receiving the events
is obscuring the element that was requested clicked.
"""
pass class InsecureCertificateException(WebDriverException):
"""
Navigation caused the user agent to hit a certificate warning, which is usually the result
of an expired or invalid TLS certificate.
"""
pass class InvalidCoordinatesException(WebDriverException):
"""
The coordinates provided to an interactions operation are invalid.
"""
pass class InvalidSessionIdException(WebDriverException):
"""
Occurs if the given session id is not in the list of active sessions, meaning the session
either does not exist or that it's not active.
"""
pass class SessionNotCreatedException(WebDriverException):
"""
A new session could not be created.
"""
pass class UnknownMethodException(WebDriverException):
"""
The requested command matched a known URL but did not match an method for that URL.
"""
pass

common/exceptions.py

3.操作浏览器常会用到如下模块

4.原理

  1.selenium client(python等语言编写的自动化测试脚本)初始化一个service服务,通过Webdriver启动浏览器驱动程序chromedriver.exe
  2.通过RemoteWebDriver向浏览器驱动程序发送HTTP请求,浏览器驱动程序解析请求,打开浏览器,并获得sessionid,如果再次对浏览器操作需携带此id
  3.打开浏览器,绑定特定的端口,把启动后的浏览器作为webdriver的remote server
  3.打开浏览器后,所有的selenium的操作(访问地址,查找元素等)均通过RemoteConnection链接到remote server,然后使用execute方法调用_request方法通过urlib3向remote server发送请求
  4.浏览器通过请求的内容执行对应动作
  5.浏览器再把执行的动作结果通过浏览器驱动程序返回给测试脚本

selenium-模块概述(1)的更多相关文章

  1. XAF-通知模块概述 web+win

    通知模块概述 1.支持 WinForms和ASP.NET程序. 2.支持调度模块或自定义业务对象. 3.功能:在指定的时间,弹出一个窗口,用户可以查看提醒.也可以取消或推迟. 如需演示项目的源码,可以 ...

  2. spark概念、编程模型和模块概述

    http://blog.csdn.net/pipisorry/article/details/50931274 spark基本概念 Spark一种与 Hadoop 相似的通用的集群计算框架,通过将大量 ...

  3. 爬虫基础(三)-----selenium模块应用程序

    摆脱穷人思维 <三> :  培养"目标导向"的思维:  好项目永远比钱少,只要目标正确,钱总有办法解决. 一 selenium模块 什么是selenium?seleni ...

  4. 爬虫之selenium模块

    Selenium 简介 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟 ...

  5. 使用Selenium模块报错的解决办法 (FileNotFound,WebDriverException)

    添加Chrome浏览器程序的目录到系统Path变量中: C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application ,使用pip3 inst ...

  6. Python爬虫——selenium模块

    selenium模块介绍 selenium最初是一个测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览 ...

  7. python 全栈开发,Day136(爬虫系列之第3章-Selenium模块)

    一.Selenium 简介 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全 ...

  8. 三: 爬虫之selenium模块

    一 selenium模块 什么是selenium?selenium是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作. selenium最初是一个自动化测试工具, ...

  9. 7 selenium 模块

    selenium 模块 一.简介 1.Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作. 2.自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接 ...

  10. 03 爬虫之selenium模块

    selenium模块 1.概念,了解selenium 什么是selenium?selenium是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作. seleniu ...

随机推荐

  1. CTC安装错误之:binding.cpp:6:29: fatal error: torch/extension.h: No such file or directory

    错误原因:该问题主要由于CTC的版本导致. 解决方法: 在终端打开warp-ctc文件夹: cd warp-ctc 然后:git checkout ac045b6072b9bc3454fb9f9f17 ...

  2. [Vue warn]: Duplicate keys detected: 'area'. This may cause an update error.

    运行vue程序,浏览器报错: 原因:检测到重复的密钥:'area',因为在使用v-for循环绑定的时候,key的值是唯一的,不能相同,否则会出现意想不到的bug 解决办法:v-for时绑定的key唯一

  3. 第八届蓝桥杯java b组第十题

    标题: k倍区间 给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1, ... Aj(i <= j)之和是K的倍数,我们就称这个区间[i, j]是K倍 ...

  4. jupyter编辑快捷键

    Jupyter笔记本有两种不同的键盘输入模式. 编辑模式允许您将代码或文本输入到一个单元格中,并通过一个绿色的单元格来表示 命令模式将键盘与笔记本级命令绑定在一起,并通过一个灰色的单元格边界显示,该边 ...

  5. Android Studio [真机测试/开发者模式]

    一.手机设置 首先根据自己的手机型号百度打开开发者模式, 我的是vivo,设置--->更多设置-->关于手机-->软件版本号连续点击会提示开启开发者模式. 并在开发者选项里打开USB ...

  6. Maven 梳理 - maven新建web项目提示"javax.servlet.http.HttpServlet" was not found on the Java Build Path

    方法一: <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api&l ...

  7. java8 运算语法集

    1.分组并进行求和组合运算 示例主要代码: List<String> items = Arrays.asList("apple", "apple", ...

  8. 浅析java垃圾回收机制

    什么是java程序中的垃圾?什么这些垃圾又是怎样被回收的?为什么会被回收?不进行回收又会怎样?这些问题都是我们要在这篇博客中要解决的问题! 大家都知道,在c语言中,作为程序员,必须得考虑到去怎样回收已 ...

  9. Nginx+Tomcat Https SSL部署方案

    1.软件版本: nginx-1.15+ tomcat-8.0+ 2.先解决一个疑问:Tomcat到底需不需要配置SSL? 答案:不需要 3.SSL申请 使用腾讯云.阿里云的服务器,会更加方便申请.(申 ...

  10. Angular2+之使用FormGroup、FormBuilder和Validators对象控制表单(取值、赋值、校验和是否可编辑等)

    1.要使用Angular自带的表单控制需要先引入相关模块(.ts文件): import { FormGroup, //表单对象类 FormBuilder, //表单生成工具类 Validators} ...