# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License. import base64
import hashlib
import os
import pkgutil
import warnings
import zipfile from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.utils import keys_to_typing
from .command import Command # Python 3 imports
try:
str = basestring
except NameError:
pass try:
from StringIO import StringIO as IOStream
except ImportError: # 3+
from io import BytesIO as IOStream # not relying on __package__ here as it can be `None` in some situations (see #4558)
_pkg = '.'.join(__name__.split('.')[:-1])
getAttribute_js = pkgutil.get_data(_pkg, 'getAttribute.js').decode('utf8')
isDisplayed_js = pkgutil.get_data(_pkg, 'isDisplayed.js').decode('utf8') class WebElement(object):
"""Represents a DOM element. Generally, all interesting operations that interact with a document will be
performed through this interface. All method calls will do a freshness check to ensure that the element
reference is still valid. This essentially determines whether or not the
element is still attached to the DOM. If this test fails, then an
``StaleElementReferenceException`` is thrown, and all future calls to this
instance will fail.""" def __init__(self, parent, id_, w3c=False):
self._parent = parent
self._id = id_
self._w3c = w3c def __repr__(self):
return '<{0.__module__}.{0.__name__} (session="{1}", element="{2}")>'.format(
type(self), self._parent.session_id, self._id) @property
def tag_name(self):
"""This element's ``tagName`` property."""
return self._execute(Command.GET_ELEMENT_TAG_NAME)['value'] @property
def text(self):
"""The text of the element."""
return self._execute(Command.GET_ELEMENT_TEXT)['value'] def click(self):
"""Clicks the element."""
self._execute(Command.CLICK_ELEMENT) def submit(self):
"""Submits a form."""
if self._w3c:
form = self.find_element(By.XPATH, "./ancestor-or-self::form")
self._parent.execute_script(
"var e = arguments[0].ownerDocument.createEvent('Event');"
"e.initEvent('submit', true, true);"
"if (arguments[0].dispatchEvent(e)) { arguments[0].submit() }", form)
else:
self._execute(Command.SUBMIT_ELEMENT) def clear(self):
"""Clears the text if it's a text entry element."""
self._execute(Command.CLEAR_ELEMENT) def get_property(self, name):
"""
Gets the given property of the element. :Args:
- name - Name of the property to retrieve. Example:: text_length = target_element.get_property("text_length")
"""
try:
return self._execute(Command.GET_ELEMENT_PROPERTY, {"name": name})["value"]
except WebDriverException:
# if we hit an end point that doesnt understand getElementProperty lets fake it
return self.parent.execute_script('return arguments[0][arguments[1]]', self, name) def get_attribute(self, name):
"""Gets the given attribute or property of the element. This method will first try to return the value of a property with the
given name. If a property with that name doesn't exist, it returns the
value of the attribute with the same name. If there's no attribute with
that name, ``None`` is returned. Values which are considered truthy, that is equals "true" or "false",
are returned as booleans. All other non-``None`` values are returned
as strings. For attributes or properties which do not exist, ``None``
is returned. :Args:
- name - Name of the attribute/property to retrieve. Example:: # Check if the "active" CSS class is applied to an element.
is_active = "active" in target_element.get_attribute("class") """ attributeValue = ''
if self._w3c:
attributeValue = self.parent.execute_script(
"return (%s).apply(null, arguments);" % getAttribute_js,
self, name)
else:
resp = self._execute(Command.GET_ELEMENT_ATTRIBUTE, {'name': name})
attributeValue = resp.get('value')
if attributeValue is not None:
if name != 'value' and attributeValue.lower() in ('true', 'false'):
attributeValue = attributeValue.lower()
return attributeValue def is_selected(self):
"""Returns whether the element is selected. Can be used to check if a checkbox or radio button is selected.
"""
return self._execute(Command.IS_ELEMENT_SELECTED)['value'] def is_enabled(self):
"""Returns whether the element is enabled."""
return self._execute(Command.IS_ELEMENT_ENABLED)['value'] def find_element_by_id(self, id_):
"""Finds element within this element's children by ID. :Args:
- id\_ - ID of child element to locate. :Returns:
- WebElement - the element if it was found :Raises:
- NoSuchElementException - if the element wasn't found :Usage:
foo_element = element.find_element_by_id('foo')
"""
return self.find_element(by=By.ID, value=id_) def find_elements_by_id(self, id_):
"""Finds a list of elements within this element's children by ID.
Will return a list of webelements if found, or an empty list if not. :Args:
- id\_ - Id of child element to find. :Returns:
- list of WebElement - a list with elements if any was found. An
empty list if not :Usage:
elements = element.find_elements_by_id('foo')
"""
return self.find_elements(by=By.ID, value=id_) def find_element_by_name(self, name):
"""Finds element within this element's children by name. :Args:
- name - name property of the element to find. :Returns:
- WebElement - the element if it was found :Raises:
- NoSuchElementException - if the element wasn't found :Usage:
element = element.find_element_by_name('foo')
"""
return self.find_element(by=By.NAME, value=name) def find_elements_by_name(self, name):
"""Finds a list of elements within this element's children by name. :Args:
- name - name property to search for. :Returns:
- list of webelement - a list with elements if any was found. an
empty list if not :Usage:
elements = element.find_elements_by_name('foo')
"""
return self.find_elements(by=By.NAME, value=name) def find_element_by_link_text(self, link_text):
"""Finds element within this element's children by visible link text. :Args:
- link_text - Link text string to search for. :Returns:
- WebElement - the element if it was found :Raises:
- NoSuchElementException - if the element wasn't found :Usage:
element = element.find_element_by_link_text('Sign In')
"""
return self.find_element(by=By.LINK_TEXT, value=link_text) def find_elements_by_link_text(self, link_text):
"""Finds a list of elements within this element's children by visible link text. :Args:
- link_text - Link text string to search for. :Returns:
- list of webelement - a list with elements if any was found. an
empty list if not :Usage:
elements = element.find_elements_by_link_text('Sign In')
"""
return self.find_elements(by=By.LINK_TEXT, value=link_text) def find_element_by_partial_link_text(self, link_text):
"""Finds element within this element's children by partially visible link text. :Args:
- link_text: The text of the element to partially match on. :Returns:
- WebElement - the element if it was found :Raises:
- NoSuchElementException - if the element wasn't found :Usage:
element = element.find_element_by_partial_link_text('Sign')
"""
return self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text) def find_elements_by_partial_link_text(self, link_text):
"""Finds a list of elements within this element's children by link text. :Args:
- link_text: The text of the element to partial match on. :Returns:
- list of webelement - a list with elements if any was found. an
empty list if not :Usage:
elements = element.find_elements_by_partial_link_text('Sign')
"""
return self.find_elements(by=By.PARTIAL_LINK_TEXT, value=link_text) def find_element_by_tag_name(self, name):
"""Finds element within this element's children by tag name. :Args:
- name - name of html tag (eg: h1, a, span) :Returns:
- WebElement - the element if it was found :Raises:
- NoSuchElementException - if the element wasn't found :Usage:
element = element.find_element_by_tag_name('h1')
"""
return self.find_element(by=By.TAG_NAME, value=name) def find_elements_by_tag_name(self, name):
"""Finds a list of elements within this element's children by tag name. :Args:
- name - name of html tag (eg: h1, a, span) :Returns:
- list of WebElement - a list with elements if any was found. An
empty list if not :Usage:
elements = element.find_elements_by_tag_name('h1')
"""
return self.find_elements(by=By.TAG_NAME, value=name) def find_element_by_xpath(self, xpath):
"""Finds element by xpath. :Args:
- xpath - xpath of element to locate. "//input[@class='myelement']" Note: The base path will be relative to this element's location. This will select the first link under this element. :: myelement.find_element_by_xpath(".//a") However, this will select the first link on the page. :: myelement.find_element_by_xpath("//a") :Returns:
- WebElement - the element if it was found :Raises:
- NoSuchElementException - if the element wasn't found :Usage:
element = element.find_element_by_xpath('//div/td[1]')
"""
return self.find_element(by=By.XPATH, value=xpath) def find_elements_by_xpath(self, xpath):
"""Finds elements within the element by xpath. :Args:
- xpath - xpath locator string. Note: The base path will be relative to this element's location. This will select all links under this element. :: myelement.find_elements_by_xpath(".//a") However, this will select all links in the page itself. :: myelement.find_elements_by_xpath("//a") :Returns:
- list of WebElement - a list with elements if any was found. An
empty list if not :Usage:
elements = element.find_elements_by_xpath("//div[contains(@class, 'foo')]") """
return self.find_elements(by=By.XPATH, value=xpath) def find_element_by_class_name(self, name):
"""Finds element within this element's children by class name. :Args:
- name: The class name of the element to find. :Returns:
- WebElement - the element if it was found :Raises:
- NoSuchElementException - if the element wasn't found :Usage:
element = element.find_element_by_class_name('foo')
"""
return self.find_element(by=By.CLASS_NAME, value=name) def find_elements_by_class_name(self, name):
"""Finds a list of elements within this element's children by class name. :Args:
- name: The class name of the elements to find. :Returns:
- list of WebElement - a list with elements if any was found. An
empty list if not :Usage:
elements = element.find_elements_by_class_name('foo')
"""
return self.find_elements(by=By.CLASS_NAME, value=name) def find_element_by_css_selector(self, css_selector):
"""Finds element within this element's children by CSS selector. :Args:
- css_selector - CSS selector string, ex: 'a.nav#home' :Returns:
- WebElement - the element if it was found :Raises:
- NoSuchElementException - if the element wasn't found :Usage:
element = element.find_element_by_css_selector('#foo')
"""
return self.find_element(by=By.CSS_SELECTOR, value=css_selector) def find_elements_by_css_selector(self, css_selector):
"""Finds a list of elements within this element's children by CSS selector. :Args:
- css_selector - CSS selector string, ex: 'a.nav#home' :Returns:
- list of WebElement - a list with elements if any was found. An
empty list if not :Usage:
elements = element.find_elements_by_css_selector('.foo')
"""
return self.find_elements(by=By.CSS_SELECTOR, value=css_selector) def send_keys(self, *value):
"""Simulates typing into the element. :Args:
- value - A string for typing, or setting form fields. For setting
file inputs, this could be a local file path. Use this to send simple key events or to fill out form fields:: form_textfield = driver.find_element_by_name('username')
form_textfield.send_keys("admin") This can also be used to set file inputs. :: file_input = driver.find_element_by_name('profilePic')
file_input.send_keys("path/to/profilepic.gif")
# Generally it's better to wrap the file path in one of the methods
# in os.path to return the actual path to support cross OS testing.
# file_input.send_keys(os.path.abspath("path/to/profilepic.gif")) """
# transfer file to another machine only if remote driver is used
# the same behaviour as for java binding
if self.parent._is_remote:
local_file = self.parent.file_detector.is_local_file(*value)
if local_file is not None:
value = self._upload(local_file) self._execute(Command.SEND_KEYS_TO_ELEMENT,
{'text': "".join(keys_to_typing(value)),
'value': keys_to_typing(value)}) # RenderedWebElement Items
def is_displayed(self):
"""Whether the element is visible to a user."""
# Only go into this conditional for browsers that don't use the atom themselves
if self._w3c:
return self.parent.execute_script(
"return (%s).apply(null, arguments);" % isDisplayed_js,
self)
else:
return self._execute(Command.IS_ELEMENT_DISPLAYED)['value'] @property
def location_once_scrolled_into_view(self):
"""THIS PROPERTY MAY CHANGE WITHOUT WARNING. Use this to discover
where on the screen an element is so that we can click it. This method
should cause the element to be scrolled into view. Returns the top lefthand corner location on the screen, or ``None`` if
the element is not visible. """
if self._w3c:
old_loc = self._execute(Command.W3C_EXECUTE_SCRIPT, {
'script': "arguments[0].scrollIntoView(true); return arguments[0].getBoundingClientRect()",
'args': [self]})['value']
return {"x": round(old_loc['x']),
"y": round(old_loc['y'])}
else:
return self._execute(Command.GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW)['value'] @property
def size(self):
"""The size of the element."""
size = {}
if self._w3c:
size = self._execute(Command.GET_ELEMENT_RECT)['value']
else:
size = self._execute(Command.GET_ELEMENT_SIZE)['value']
new_size = {"height": size["height"],
"width": size["width"]}
return new_size def value_of_css_property(self, property_name):
"""The value of a CSS property."""
return self._execute(Command.GET_ELEMENT_VALUE_OF_CSS_PROPERTY, {
'propertyName': property_name})['value'] @property
def location(self):
"""The location of the element in the renderable canvas."""
if self._w3c:
old_loc = self._execute(Command.GET_ELEMENT_RECT)['value']
else:
old_loc = self._execute(Command.GET_ELEMENT_LOCATION)['value']
new_loc = {"x": round(old_loc['x']),
"y": round(old_loc['y'])}
return new_loc @property
def rect(self):
"""A dictionary with the size and location of the element."""
if self._w3c:
return self._execute(Command.GET_ELEMENT_RECT)['value']
else:
rect = self.size.copy()
rect.update(self.location)
return rect @property
def screenshot_as_base64(self):
"""
Gets the screenshot of the current element as a base64 encoded string. :Usage:
img_b64 = element.screenshot_as_base64
"""
return self._execute(Command.ELEMENT_SCREENSHOT)['value'] @property
def screenshot_as_png(self):
"""
Gets the screenshot of the current element as a binary data. :Usage:
element_png = element.screenshot_as_png
"""
return base64.b64decode(self.screenshot_as_base64.encode('ascii')) def screenshot(self, filename):
"""
Saves a screenshot of the current element to a PNG image file. Returns
False if there is any IOError, else returns True. Use full paths in
your filename. :Args:
- filename: The full path you wish to save your screenshot to. This
should end with a `.png` extension. :Usage:
element.screenshot('/Screenshots/foo.png')
"""
if not filename.lower().endswith('.png'):
warnings.warn("name used for saved screenshot does not match file "
"type. It should end with a `.png` extension", UserWarning)
png = self.screenshot_as_png
try:
with open(filename, 'wb') as f:
f.write(png)
except IOError:
return False
finally:
del png
return True @property
def parent(self):
"""Internal reference to the WebDriver instance this element was found from."""
return self._parent @property
def id(self):
"""Internal ID used by selenium. This is mainly for internal use. Simple use cases such as checking if 2
webelements refer to the same element, can be done using ``==``:: if element1 == element2:
print("These 2 are equal") """
return self._id def __eq__(self, element):
return hasattr(element, 'id') and self._id == element.id def __ne__(self, element):
return not self.__eq__(element) # Private Methods
def _execute(self, command, params=None):
"""Executes a command against the underlying HTML element. Args:
command: The name of the command to _execute as a string.
params: A dictionary of named parameters to send with the command. Returns:
The command's JSON response loaded into a dictionary object.
"""
if not params:
params = {}
params['id'] = self._id
return self._parent.execute(command, params) def find_element(self, by=By.ID, value=None):
"""
Find an element given a By strategy and locator. Prefer the find_element_by_* methods when
possible. :Usage:
element = element.find_element(By.ID, 'foo') :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_CHILD_ELEMENT,
{"using": by, "value": value})['value'] def find_elements(self, by=By.ID, value=None):
"""
Find elements given a By strategy and locator. Prefer the find_elements_by_* methods when
possible. :Usage:
element = element.find_elements(By.CLASS_NAME, 'foo') :rtype: list of 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_CHILD_ELEMENTS,
{"using": by, "value": value})['value'] def __hash__(self):
return int(hashlib.md5(self._id.encode('utf-8')).hexdigest(), 16) def _upload(self, filename):
fp = IOStream()
zipped = zipfile.ZipFile(fp, 'w', zipfile.ZIP_DEFLATED)
zipped.write(filename, os.path.split(filename)[1])
zipped.close()
content = base64.encodestring(fp.getvalue())
if not isinstance(content, str):
content = content.decode('utf-8')
try:
return self._execute(Command.UPLOAD_FILE, {'file': content})['value']
except WebDriverException as e:
if "Unrecognized command: POST" in e.__str__():
return filename
elif "Command not found: POST " in e.__str__():
return filename
elif '{"status":405,"value":["GET","HEAD","DELETE"]}' in e.__str__():
return filename
else:
raise e

selenium的方法的更多相关文章

  1. python+selenium安装方法

    一.准备工具: 下载 python[python 开发环境] http://python.org/getit/ 下载 setuptools [python 的基础包工具] http://pypi.py ...

  2. Java&Selenium截图方法封装

    Java&Selenium截图方法封装 package util; import org.apache.commons.io.FileUtils; import org.openqa.sele ...

  3. selenium定位方法实例

    selenium定位方法实例 首先打开浏览器输入微博的网址,将网页最大化,等待3秒 from selenium import webdriver import time driver = webdri ...

  4. selenium定位方法(二)

    selenium定位方法(二)  1.xpath定位:xpath是在XML中查找节点所在的路径的表达式 1)绝对路径的Xpath表达式 例:/html/body/div/div[1]/ul//li[3 ...

  5. selenium定位方法(一)

    selenium定位方法-(一) 1.定位页面元素的方式(By类的方法) 1)id定位:通过页面元素的id属性值来定位一个页面元素       注意事项:如果每次刷新网页之后元素的id属性值都不同,说 ...

  6. Selenium switch_to方法

    在web应用自动化测试中,点击一个链接或者按钮会打开一个新的浏览器窗口,会出现多个窗口实例.默认情况下的焦点在主窗口(父窗口),如果要对子窗口进行操作,就需要首先切换到子窗口. Selenium We ...

  7. python环境安装selenium和手动下载安装selenium的方法

    方法1:cmd环境下,用pip install selenium 可能会很慢 方法2:下载selenium安装包手动安装 下载地址:https://pypi.org/project/selenium/ ...

  8. python selenium wait方法

    遇到一个网站运行很慢,所以要等待某个元素显示出来之后再进行操作,自己手上的书上没有例子可以直接用 发现一篇文章:http://www.cnblogs.com/yoyoketang/p/6517477. ...

  9. selenium定位方法(java实例)(二)

    从下图中可以看到selenium有三类定位元素的方法,一种是直接通过属性.标签以及链接来定位,一种是XPath方式,最后一种是CSS方式. 下表列举了元素定位的例子 selenium之页面元素定位方法 ...

  10. python selenium webdriver方法封装(find_element_by)

    下面是对find_element_by_就行了封装,封装之后的高级方法就是getElement() 下面是具体的代码: def getElement(self, selector): "&q ...

随机推荐

  1. 【QT 学习笔记】 一、 VS2015+ QT环境安装

    1.   安装    qt-opensource-windows-x86-msvc2015_64-5.6.0.exe   (根据自己的VS版本来安装) 下载地址 http://download.qt. ...

  2. 爬虫遇到IP访问频率限制的解决方案

    背景: 大多数情况下,我们遇到的是访问频率限制.如果你访问太快了,网站就会认为你不是一个人.这种情况下需要设定好频率的阈值,否则有可能误伤.如果大家考过托福,或者在12306上面买过火车票,你应该会有 ...

  3. web开发:jquery高级

    一.jq选择器 二.属性操作 三.jq轮播图 四.样式操作 五.jq动事件 六.jq动画 七.自定义动画 八.jq动画案例 一.jq选择器 - css3语法选择器 ```js$('css3选择器位') ...

  4. curl命令的高级用法

    curl命令 是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具.作为一款强力工具,curl支持包括HTTP.HTTPS. ...

  5. HDU4465 Candy

    Candy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. [SCOI2016]美味——主席树+按位贪心

    原题戳这里 题解 让异或值最大显然要按位贪心,然后我们还发现加上一个\(x_i\)的效果就是所有\(a_i\)整体向右偏移了,我们对于\({a_i}\)开个主席树,支持查询一个区间中有多少个在\([L ...

  7. Cow Hopscotch (单调队列 + DP)

    链接:https://ac.nowcoder.com/acm/contest/1113/K来源:牛客网 The cows have reverted to their childhood and ar ...

  8. (二)线程Thread中的方法详解

    1.start() start()方法的作用讲得直白点就是通知"线程规划器",此线程可以运行了,正在等待CPU调用线程对象得run()方法,产生一个异步执行的效果.通过start( ...

  9. 基于IAP的STM32程序更新技术

    引言 嵌入式系统的开发最终需要将编译好的代码下载到具体的微控制器芯片上,而不同厂家的微控制器芯片有不同的下载方式.随着技术的发展和应用需求的更新,用户程序加载趋向于在线编程的方式,越来越多的芯片公司提 ...

  10. 从mysql8.0读取数据并形成pandas dataframe类型数据,精确定位行列式中的元素,并读取

    from pandas import * import pandas as pd from sqlalchemy import create_engine engine = create_engine ...