selenium之python源码解读-webdriver继承关系
一、webdriver继承关系
在selenium中,无论是常用的Firefox Driver 还是Chrome Driver和Ie Drive,他们都继承至selenium\webdriver\remote下webdriver.py中的WebDriver 类,如下
chrome WebDriver
selenium\webdriver\chrome下webdriver.py中WebDriver定义如下
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver class WebDriver(RemoteWebDriver):
"""
Controls the ChromeDriver and allows you to drive the browser.
"""
firefox WebDriver
selenium\webdriver\firefox 下webdriver.py中WebDriver定义如下
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver class WebDriver(RemoteWebDriver):
pass
ie WebDriver
selenium\webdriver\ie 下webdriver.py中WebDriver定义如下
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
class WebDriver(RemoteWebDriver):
def __init__(self, executable_path='IEDriverServer.exe', capabilities=None,
port=DEFAULT_PORT, timeout=DEFAULT_TIMEOUT, host=DEFAULT_HOST,
log_level=DEFAULT_LOG_LEVEL, log_file=DEFAULT_LOG_FILE):
......
如上源码:from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver 都是继承至RemoteWebDriver ,并主要重写__init__方法
其他方法主要继承至父类RemoteWebDriver ,因此着重看下RemoteWebDriver 类中的方法
1、find类
编写脚本常用的查找页面元素方法
def find_element_by_id(self, id_):
#Finds an element by id.'''
pass
def find_elements_by_id(self, id_):
#Finds multiple elements by id.
pass
def find_element_by_xpath(self, xpath):
#Finds an element by xpath.
pass
def find_elements_by_xpath(self, xpath):
#Finds multiple elements by xpath.
pass
def find_element_by_link_text(self, link_text):
#Finds an element by link text
pass
def find_elements_by_link_text(self, text):
#Finds elements by link text.
pass
def find_element_by_partial_link_text(self, link_text):
#Finds elements by a partial match of their link text.
pass
def find_elements_by_partial_link_text(self, link_text):
#Finds an element by a partial match of its link text.
pass
def find_element_by_name(self, name):
#Finds an element by name.
pass
def find_elements_by_name(self, name):
#Finds elements by name.
pass
def find_element_by_tag_name(self, name):
#Finds an element by tag name.
pass
def find_elements_by_tag_name(self, name):
#Finds elements by tag name.
pass
def find_element_by_class_name(self, name):
Finds an element by class name.
pass
def find_elements_by_class_name(self, name):
#Finds elements by class name.
pass
def find_element_by_css_selector(self, css_selector):
#Finds an element by css selector.
pass
def find_elements_by_css_selector(self, css_selector):
#Finds elements by css selector.
pass
def find_element(self, by=By.ID, value=None):
#'Private' method used by the find_element_by_* methods.
pass
def find_elements(self, by=By.ID, value=None):
#'Private' method used by the find_elements_by_* methods.
pass
通过查看源码,其实以上方法都是通过调用
self.find_element(by=By.XXX, value=name)或者self.find_elements(by=By.XXX, value=name)方法来重新定义的
def find_element(self, by=By.ID, value=None):
"""
'Private' method used by the find_element_by_* methods. :Usage:
Use the corresponding find_element_by_* instead of this. :rtype: WebElement
"""
if self.w3c:
if by == By.ID:
by = By.CSS_SELECTOR
value = '[id="%s"]' % value
elif by == By.TAG_NAME:
by = By.CSS_SELECTOR
elif by == By.CLASS_NAME:
by = By.CSS_SELECTOR
value = ".%s" % value
elif by == By.NAME:
by = By.CSS_SELECTOR
value = '[name="%s"]' % value
return self.execute(Command.FIND_ELEMENT, {
'using': by,
'value': value})['value']
其中by.XXX是selenium\webdriver\common下by.py文件中By类定义的静态常量
class By(object):
"""
Set of supported locator strategies.
""" ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
通过以上分析,不难发现,只要掌握了self.find_element(by=By.XXX, value=name)或者self.find_elements(by=By.XXX, value=name)方法,已就意味着掌握了全部的查找定位页面元素的方法
2、除了常用的find类方法外,以下方法在编写脚本是也是常用的
ef get(self, url):
"""
Loads a web page in the current browser session.
""" @property
def title(self):
"""Returns the title of the current page.""" @property
def current_url(self):
"""Gets the URL of the current page.""" @property
def current_window_handle(self):
"""Returns the handle of the current window.""" def maximize_window(self):
"""Maximizes the current window that webdriver is using""" @property
def switch_to(self):
return self._switch_to # Target Locators
def switch_to_active_element(self):
""" Deprecated use driver.switch_to.active_element""" def switch_to_window(self, window_name):
""" Deprecated use driver.switch_to.window""" def switch_to_frame(self, frame_reference):
""" Deprecated use driver.switch_to.frame""" def switch_to_default_content(self):
""" Deprecated use driver.switch_to.default_content""" def switch_to_alert(self):
""" Deprecated use driver.switch_to.alert"""
其中使用@property修饰的,可以当作为属性来使用,如driver.current_url
3、为什么在实际应用过程中通过from selenium import webdriver引入webdriver,然后通过webdriver.Chrome()就可以实例化Chrome的Driver对象呢?
从以上selenium目录结构,理论上需要通过以下来导入
#导入chrome的WebDriver
from selenium.webdriver.chrome.webdriver import WebDriver
#导入firefox的WebDriver
from selenium.webdriver.firefox.webdriver import WebDriver
#导入ie的WebDriver
from selenium.webdriver.ie.webdriver import WebDriver
selenium项目目录结构
selenium
│ __init__.py
│
├─common
│ │ exceptions.py
│ │ __init__.py
│
├─webdriver
│ │ __init__.py
│ │
│ ├─android
│ │ │ webdriver.py
│ │ │ __init__.py
│ │
│ ├─blackberry
│ │ │ webdriver.py
│ │ │ __init__.py
│ │
│ ├─chrome
│ │ │ options.py
│ │ │ remote_connection.py
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ │
│ ├─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
│ │ │
│ │ │
│ │ ├─html5
│ │ │ │ application_cache.py
│ │ │ │ __init__.py
│ │
│ ├─edge
│ │ │ options.py
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ ├─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
│ │
│ ├─ie
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ ├─opera
│ │ │ options.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ ├─phantomjs
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ ├─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
│ ├─safari
│ │ │ service.py
│ │ │ webdriver.py
│ │ │ __init__.py
│ ├─support
│ │ │ abstract_event_listener.py
│ │ │ color.py
│ │ │ events.py
│ │ │ event_firing_webdriver.py
│ │ │ expected_conditions.py
│ │ │ select.py
│ │ │ ui.py
│ │ │ wait.py
│ │ │ __init__.py
通过查看selenium\webdriver下__init__.py文件发现
from .firefox.webdriver import WebDriver as Firefox # noqa
from .firefox.firefox_profile import FirefoxProfile # noqa
from .chrome.webdriver import WebDriver as Chrome # noqa
from .chrome.options import Options as ChromeOptions # noqa
from .ie.webdriver import WebDriver as Ie # noqa
其实是因为该出已经导入了,所以才可以直接使用Firefox、Chrome
selenium之python源码解读-webdriver继承关系的更多相关文章
- selenium之python源码解读-expected_conditions
一.expected_conditions 之前在 selenium之python源码解读-WebDriverWait 中说到,until方法中method参数,需要传入一个function对象,如果 ...
- selenium之python源码解读-WebDriverWait
一.显示等待 所谓显示等待,是针对某一个特定的元素设置等待时间,如果在规定的时间内找到了该元素,就执行相关的操作,如果在规定的时间内没有找到该元素,在抛出异常 PS:注意显示等待和隐身等待的区别,隐身 ...
- 如何判断一个Http Message的结束——python源码解读
HTTP/1.1 默认的连接方式是长连接,不能通过简单的TCP连接关闭判断HttpMessage的结束. 以下是几种判断HttpMessage结束的方式: 1. HTTP协议约定status ...
- python 源码解读2
http://www.jianshu.com/users/4d4a2f26740b/latest_articles http://blog.csdn.net/ssjhust123/article/ca ...
- DRF(1) - REST、DRF(View源码解读、APIView源码解读)
一.REST 1.什么是编程? 数据结构和算法的结合. 2.什么是REST? 首先回顾我们曾经做过的图书管理系统,我们是这样设计url的,如下: /books/ /get_all_books/ 访问所 ...
- REST、DRF(View源码解读、APIView源码解读)
一 . REST 前言 1 . 编程 : 数据结构和算法的结合 .小程序如简单的计算器,我们输入初始数据,经过计算,得到最终的数据,这个过程中,初始数据和结果数据都是数据,而计算 ...
- Restful 1 -- REST、DRF(View源码解读、APIView源码解读)及框架实现
一.REST 1.什么是编程? 数据结构和算法的结合 2.什么是REST? - url用来唯一定位资源,http请求方式来区分用户行为 首先回顾我们曾经做过的图书管理系统,我们是这样设计url的,如下 ...
- SDWebImage源码解读之SDWebImageDownloaderOperation
第七篇 前言 本篇文章主要讲解下载操作的相关知识,SDWebImageDownloaderOperation的主要任务是把一张图片从服务器下载到内存中.下载数据并不难,如何对下载这一系列的任务进行设计 ...
- SDWebImage源码解读之SDWebImageCache(下)
第六篇 前言 我们在SDWebImageCache(上)中了解了这个缓存类大概的功能是什么?那么接下来就要看看这些功能是如何实现的? 再次强调,不管是图片的缓存还是其他各种不同形式的缓存,在原理上都极 ...
随机推荐
- TensorSpace:超酷炫3D神经网络可视化框架
TensorSpace:超酷炫3D神经网络可视化框架 TensorSpace - 一款 3D 模型可视化框架,支持多种模型,帮助你可视化层间输出,更直观地展示模型的输入输出,帮助理解模型结构和输出方法 ...
- PC电脑看电视 / 电视直播 / 高清频道 / 直播源
打开方式 PotPlayer + .m3u播放列表 效果图 .m3u播放列表 #EXTM3U #EXTINF:150,CCTV1综合 http://183.251.61.207/PLTV/888888 ...
- [DevExpress] - 在 DataGrid 中添加多选复选框的方法
设置方法 在 GridView 中设置 OptionSelection 属性如下: 效果 参考资料 https://stackoverflow.com/a/9078848http://blog.csd ...
- cesium 水面、淹没 效果
水面效果 参考: http://cesiumcn.org/topic/158.html http://api.rivermap.cn/cesium/rivermap/map.html https:// ...
- Java中的事务及使用
什么是事务? 事务(Transaction),一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常由高级数据库操纵语言或编程语言(如S ...
- hdu 6562 Lovers (线段树)
大意: 有$n$个数字串, 初始为空, 两种操作(1)把$[l,r]$范围的所有数字串首位添加数位$d$ (2)询问$[l,r]$区间和 假设添加的数为$L$, $L$位数为$H$, $L$翻转后乘上 ...
- PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...
- shell 学习笔记8-case条件语句
一.case语句简介 1.什么是case条件语句 case条件语句就相当于多分支的if/elif/else条件语句,但是比这样的语句更规范更好看,经常被用在失效系统服务启动脚本等企业应用中 程序将ca ...
- js取得对象的类名constructor.name
constructor 属性返回对创建此对象的数组函数的引用. 语法 object.constructor 例子 new Array().constructor//ƒ Array() { [nativ ...
- sql语句中包含引号处理方法
1. 背景 在使用Python脚本向数据库导入日志文件时候,突然报错. 2. 解决思路 查看messages文件,发现有一条语句里包含单引号. 查看sql语句,是使用单引号标注str类型. 3. 得出 ...