python和selenium的关系_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 importWebDriver as RemoteWebDriverclassWebDriver(RemoteWebDriver):"""Controls the ChromeDriver and allows you to drive the browser."""
firefox WebDriver
selenium\webdriver\firefox 下webdriver.py中WebDriver定义如下
from selenium.webdriver.remote.webdriver importWebDriver as RemoteWebDriverclassWebDriver(RemoteWebDriver):pass
ie WebDriver
selenium\webdriver\ie 下webdriver.py中WebDriver定义如下
from selenium.webdriver.remote.webdriver importWebDriver as RemoteWebDriverclassWebDriver(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类
编写脚本常用的查找页面元素方法
deffind_element_by_id(self, id_):#Finds an element by id.'''
pass
deffind_elements_by_id(self, id_):#Finds multiple elements by id.
pass
deffind_element_by_xpath(self, xpath):#Finds an element by xpath.
pass
deffind_elements_by_xpath(self, xpath):#Finds multiple elements by xpath.
pass
deffind_element_by_link_text(self, link_text):#Finds an element by link text
pass
deffind_elements_by_link_text(self, text):#Finds elements by link text.
pass
deffind_element_by_partial_link_text(self, link_text):#Finds elements by a partial match of their link text.
pass
deffind_elements_by_partial_link_text(self, link_text):#Finds an element by a partial match of its link text.
pass
deffind_element_by_name(self, name):#Finds an element by name.
pass
deffind_elements_by_name(self, name):#Finds elements by name.
pass
deffind_element_by_tag_name(self, name):#Finds an element by tag name.
pass
deffind_elements_by_tag_name(self, name):#Finds elements by tag name.
pass
deffind_element_by_class_name(self, name):
Finds an element byclassname.pass
deffind_elements_by_class_name(self, name):#Finds elements by class name.
pass
deffind_element_by_css_selector(self, css_selector):#Finds an element by css selector.
pass
deffind_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"""
ifself.w3c:if by ==By.ID:
by=By.CSS_SELECTOR
value= '[id="%s"]' %valueelif by ==By.TAG_NAME:
by=By.CSS_SELECTORelif by ==By.CLASS_NAME:
by=By.CSS_SELECTOR
value= ".%s" %valueelif by ==By.NAME:
by=By.CSS_SELECTOR
value= '[name="%s"]' %valuereturnself.execute(Command.FIND_ELEMENT, {'using': by,'value': value})['value']
其中by.XXX是selenium\webdriver\common下by.py文件中By类定义的静态常量
classBy(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."""@propertydeftitle(self):"""Returns the title of the current page."""@propertydefcurrent_url(self):"""Gets the URL of the current page."""@propertydefcurrent_window_handle(self):"""Returns the handle of the current window."""
defmaximize_window(self):"""Maximizes the current window that webdriver is using"""@propertydefswitch_to(self):returnself._switch_to#Target Locators
defswitch_to_active_element(self):"""Deprecated use driver.switch_to.active_element"""
defswitch_to_window(self, window_name):"""Deprecated use driver.switch_to.window"""
defswitch_to_frame(self, frame_reference):"""Deprecated use driver.switch_to.frame"""
defswitch_to_default_content(self):"""Deprecated use driver.switch_to.default_content"""
defswitch_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 importWebDriver#导入firefox的WebDriver
from selenium.webdriver.firefox.webdriver importWebDriver#导入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
python和selenium的关系_selenium之python源码解读-webdriver继承关系相关推荐
- python组件的react实现_【React源码解读】- 组件的实现
前言 react使用也有一段时间了,大家对这个框架褒奖有加,但是它究竟好在哪里呢? 让我们结合它的源码,探究一二!(当前源码为react16,读者要对react有一定的了解) 回到最初 根据react ...
- Python新闻推荐系统+爬虫+可视化 大数据项目 源码下载
Python新闻推荐系统+爬虫+可视化 大数据项目 源码下载 网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题.文本.图片.视频链接 推荐算法:权重衰减+标签推荐+区域推荐+热点推 ...
- vscode编辑python时跳转到.pyi而不是源码的问题
vscode编辑python时跳转到.pyi而不是源码的问题 问题描述 最近在使用vscode编辑python代码,使用 ctrl + 鼠标左键 进行跳转到源代码时发现,编辑器跳转到了vscode自动 ...
- 100个必会的python脚本-100行Python代码实现自动抢火车票(附源码)
前言 又要过年了,今年你不妨自己写一段代码来抢回家的火车票,是不是很Cool.下面话不多说了,来一起看看详细的介绍吧. 先准备好: 12306网站用户名和密码 chrome浏览器及下载chromedr ...
- python自动化办公 百度网盘-Python自动化办公实战教程2020最新带源码
资源目录: 视频 001_Excel_xlrd读_xlwt写.mp4 003_Excel_案例_统计每位同学总分.mp4 004_MySQL_我为什么不建议学.mp4 005_Excel_更灵活的操作 ...
- python自动化办公入门书籍-Python自动化办公实战教程2020最新带源码
资源目录: 视频 001_Excel_xlrd读_xlwt写.mp4 003_Excel_案例_统计每位同学总分.mp4 004_MySQL_我为什么不建议学.mp4 005_Excel_更灵活的操作 ...
- print python 带回车_python标准库threading源码解读【二】
紧接着上一篇文章继续解析源码 甘蔗:python标准库threading源码解读[一]zhuanlan.zhihu.com 目录 Event的介绍和用法 Event源码解析 以后的内容尽量少一点并且 ...
- Python基于OpenCV的人脸表情识别系统[源码&部署教程]
1.项目背景 人脸表情识别是模式识别中一个非常重要却十分复杂的课题.首先对计算机人脸表情识别技术的研究背景及发展历程作了简单回顾.然后对近期人脸表情识别的方法进行了分类综述.通过对各种识别方法的分析与 ...
- 计算机毕业设计Python+uniapp实验室考勤小程序(小程序+源码+LW)
计算机毕业设计Python+uniapp实验室考勤小程序(小程序+源码+LW) 该项目含有源码.文档.程序.数据库.配套开发软件.软件安装教程 项目运行 环境配置: Pychram社区版+ pytho ...
最新文章
- 清华大学:2021 元宇宙研究报告!
- ignite服务中的bean注入为空
- 使用jquery实现的计算器功能
- 计算机科学中最重要的32个算法zz
- Redis的安装部署
- springboot项目文档源码_基于SpringBoot和Vue的企业级中后台项目(附源码)
- add-migration Build failed.
- linux blender骨骼绑定,在Linux系统中安装开源3D创建套件Blender的方法
- 我们每天努力上班赚钱,财富离我们很远
- 面试官问你JavaScript,直接把这篇文章甩给他
- cas与java锁_JAVA之锁-cas
- powerdesigner数据字典导出模板设置
- 常用的140个Windows XP设置 [转]
- CentOS 7.5 安装MySQL教程
- 权威披露2014微信十大最火谣言 | 调查称女性最爱传谣,你服不服?
- 安装java和scala语言开发环境
- 剧本--反内卷委员会
- 软考中级哪一门比较好过?
- 程序员接私单接出优越感:十个月赚了60多万,加工资年入百万
- C语言实现模拟大乐透和双色球彩票机选【纯娱乐】
热门文章
- 16kb等于多少b_面试官:MySQL索引为何选择B+树?
- 自学python免费教材-python零基础自学教材
- 精通python工资高吗-2020年10月程序员工资再创新高
- python和java哪个好学-java和python哪个的前途更好?
- python代码示例-《Python编程:从入门到实践》DEMO实例代码
- 安装完python怎么打开-python安装后怎么启用
- python自学免费教程-python免费入门教程/求完整的Python入门教程
- 语音识别几成标配,哈弗F7的智能语音系统有何不同?
- 使用Hbuilder将自己app发布到App Store
- 矩阵方程求解最快c语言算法,求助! C语言用矩阵求解方程组