目录
一、简介
selenium 驱动
selenium server
创建deriver实例,selenium.webdriver.chromium.webdriver.ChromiumDriver
查找元素8大方法
元素属性 selenium.webdriver.remote.webelement
切换
鼠标操作 from selenium.webdriver import ActionChains
鼠标操作 selenium.webdriver.common.touch_actions
键盘操作
获取元素信息
元素等待
命令 selenium.webdriver.remote.command
错误处理器 selenium.webdriver.remote.errorhandler
网络连接选项 selenium.webdriver.remote.mobile
下拉框 selenium.webdriver.support.select
监听器 selenium.webdriver.support.abstract_event_listener
官网:Selenium Client Driver — Selenium 3.141 documentation
selenium各模块索引地址:Python Module Index — Selenium 3.141 documentation
git地址:https://github.com/SeleniumHQ/selenium/tree/trunk/py
selenium 驱动浏览器需要安装额外的驱动软件。驱动下载地址见下表:
Chrome: |
ChromeDriver - WebDriver for Chrome - Downloads |
Edge: | Microsoft Edge Driver - Microsoft Edge Developer |
Firefox: | https://github.com/mozilla/geckodriver/releases |
Safari: | WebDriver Support in Safari 10 | WebKit |
不同浏览器版本对应的驱动版本也同,下载对应版本的浏览器驱动后,将驱动软件包放到Python的bin目录下 即可。
Firefox 插件:firebug,firepath
下载地址:http : //selenium-release.storage.googleapis.com/4.0/selenium-server-standalone-4.0.0.jar
从命令行启动服务器:然后运行您的 Python 客户端脚本。
java -jar selenium-server-standalone-4.0.0.jar
# 1,from selenium.webdriver.chromium.webdriver import ChromiumDriver
driver = ChromiumDriver()
# 设置网络速度。模拟网络环境
driver.set_network_conditions(
offline=False,
latency=5, # additional latency (ms)
download_throughput=500 * 1024, # maximal throughput
upload_throughput=500 * 1024) # maximal throughput
--------------------------------------------------------------------
from selenium import webdriver
driver = webdriver.Chrome()
# 获取网络速度设置
driver.get_network_conditions()
# 关闭浏览器,并关闭driver
driver.quit()
1,driver.find_element_by_id(id)
2,driver.find_element_by_name(name)
3, driver.find_element_by_class_name(class)
4, driver.find_element_by_link_text(链接文本)
5, driver.find_element_by_partial_link_text(部分链接文本)
6, driver.find_elemnet_by_Xpath()
6.1,路径查找:driver.find_elemnet_by_Xpath(//*input)
6.2,路径+元素属性:driver.find_elemnet_by_Xpath(//*[@id='id值'])
6.3,路径+属性+逻辑:driver.find_elemnet_by_Xpath(//*[@id='id值' and@name='name值'])
6.4,路径+属性+层级:driver.find_elemnet_by_Xpath(//*[@id='id值']/input)
6.5,文本内容:driver.find_elemnet_by_Xpath(//*[text()="文本"])
6.6,模糊匹配:driver.find_elemnet_by_Xpath(//*[contains(@id,"部分值")])
6.7,driver.find_elemnet_by_Xpath(//*[contains(text(), "")])
7, driver.find_element_by_css_selector()
driver.find_element_by_css_selector('#id')
driver.find_element_by_css_selector('.class')[0]
driver.find_element_by_css_selector("input")[0]
driver.find_element_by_css_selector("[id='username']")
driver.find_element_by_css_selector("form #q") # 后代选择器
8, driver.find_element(BY类)
#导包
from selenium.webdriver.common.by import By
driver.find_element(By.ID, "id")
driver.find_element(By.NAME, "name")
driver.find_element(By.CLASS_NAME, "class name")
driver.find_element(By.CSS_SELECTOR, "css selector")
driver.find_element(By.LINK_TEXT, "link text")
driver.find_element(By.TAG_NAME, "tag name")
driver.find_element(By.XPATH, "xpath")
driver.find_element(By.PARTIAL_LINK_TEXT, "partial link text")
另外还有一个 用来判断元素
# expected_conditions
# https://selenium-python.readthedocs.io/waits.html#waits
# https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
""" expected_conditions 方法
title_is 判断页面title是否 == 预期,返回布尔值
title_contains 判断页面title 是否包含指定的字符串,返回布尔值
presence_of_element_located 判断元素是是否被加载到了到了dom树里,可能元素不可见
visibility_of_element_located 判断元素是否可见,参数为locator
visibility_of 判断元素是否可见,参数为已经被定为了的元素
presence_of_all_elements_located 判断是否至少有一个元素存在dom树里
text_to_be_present_in_element 判断元素是否包含了某个字符串
text_to_be_present_in_element_value 判断元素的value属性是否包含了某个字符串
frame_to_be_available_and_switch_to_it 判断frame是否可以switch进去,如果可以的话,返回True,并switch进去
invisibility_of_element_located 判断元素是否可见并可点击
element_to_be_clickable 判断元素是佛可被点击
staleness_of 等元素从dom树里移除,返回True, False
element_to_be_selected 判断原始是否被选中了
element_located_to_be_selected
element_selection_state_to_be 判断元素的选中状态是否符合预期
element_located_selection_state_to_be
alert_is_present # 判断页面内是否有 alert
"""
element.clear() # 清空
element.click() # 点击
element.get_attribute(name) # 获取指定属性
element.get_property(name) # 获取元素的指定属性
element.id
element.is_displayed() # 元素是否可见
element.is_enabled() # # 元素是否可用
element.is_selected() #元素是否选中
element.location # 元素的位置
element.size # 元素大小
element.tag_name # 元素的标签名
element.text # 元素文案
element.value_of_css_property(property_name) # 元素的指定css属性值
element.send_key(*value) # 输入文本
element.submit() # 提交表单
element.screenshot_as_png # 截图,二进制
element.screenshot_as_base64 # 获取当前元素的截屏,作为base64编码的字符串
element.screenshot(file) # 截图到指定路径
element.location_once_scrolled_into_view # 此属性可能会在没有警告的情况下更改。使用它可以发现一个元素在屏幕上的位置,以便我们可以单击它。此方法将导致元素滚动到视图中。返回屏幕上左上角的位置,如果元素不可见则返回None。
提示/警告框 Alert 弹窗处理
from selenium.webdriver.common.alert import Alert
alert = Alert(Driver)
alert.accept() # 允许
alert.dismiss() # 取消
alert.text # 获取弹窗文本
alert.send_keys(keysToSend) # 输入信息
切换框架
1,driver.switch_to.frame(driver.find_element(By.ID, “id值”))
2,driver.switch_to.frame(driver.find_element(By.NAME, "name值")
3,driver.switch_to.frame(driver.find_element(By.xx, "frame属性")
4,切换回默认框架/主框架
1,driver.switch_to.default_content()
2,driver.switch_to.parent_frame()
切换窗口
1,获取当前窗口句柄 driver.current_handle
2,获取所有串口句柄 driver.window_handles
3,切换到指定窗口driver.switch_to.window(handle)
from selenium.webdriver import ActionChains
action=ActionChains(driver) # 创建实例
action.click(on_element=None) # 点击操作,默认没有输入参数时单机当前鼠标位置;输入指定要点击的元素
action.click_and_hold(on_element=None) # 按住鼠标左键不放,默认点击当前位置;
action.double_click(on_element=None) # 双击,默认当前位置;
action.context_click(on_element=None) # 右键点击,默认当前位置;
action.drag_and_drop(source, target) # 鼠标左键从一个元素按住拖动到另一个元素位置
action.drag_and_drop_bu_offset(source, xoffset, yoffset) # 鼠标左键从一个元素按住移动到目标偏移量位置并释放鼠标按钮。
action.key_down(key, on_element=None) # 仅发送按键参数,不释放。只能与修饰键(control, alt, shift)一起使用。例如:ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
action.key_up(key, on_element=None) # 释放修饰键。例如:ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
action.move_by_offset(xoffset, yoffset) # 将鼠标移动到与当前鼠标位置的偏移量。
action.move_to_element(to_element) # 将鼠标移动到元素的中间位置。
action.move_to_element_with_offset(to_element, xoffset, yoffset) # 将鼠标移动到指定元素的偏移量。偏移量相对于元素的左上角。
action.pause(second) # 在指定秒数内暂停所有输入
action.release(on_element=None) # 释放在元素上的鼠标按钮
action.reset_actions() # 清除已存储在本地和远程的操作
action.send_keys(*keys_to_send) # 将键发送到当前聚焦的元素。参数在Keys类中
action.send_keys_to_element(element, *keys_to_send) # 将键发送到指定元素。
action.perform() # 执行存储的所有动作,这一步才开始执行,之前的都是将操作存储。
from selenium.webdriver.common.touch_actions import TouchActions
touch_action = Touchction(driver)
touch_acton.double_tap(on_element) # 双击指定元素
touch_action.flick(xspeed, yspeed) # 从屏幕的随机位置开始滑动,xspeed像素/秒,yspeed像素/秒
touch_action.flick_element(on_element, xoffset, yoffset, spedd) # 从指定元素开始以每秒speed像素的速度滑动到指定偏移像素位置。
touch_action.long_press(on_element) # 长按指定元素
touch_action.move(xcoord, ycoord) # 移动当前点击的点到指定位置
touch_action.release(xcoord, ycoord) # 在指定位置释放鼠标
touch_action.scroll(xoffset, yoffset) # 点击当前鼠标位置并移动到指定位置
touch_action.scroll_from_element(on_elelment, xoffset, yoffset) # 从指定元素位置点击并移动到指定位置
touch_action.tap(on_element) # 轻敲指定元素
touch_action.tap_and_hold(xroord, ycoord) # 在指定的坐标处点击并保持按压
touch_action.perform() # 执行存储的动作
1,导包:from selenium.webdriver.ommon.keys import Keys
2,删除:element.send_keys(Keys.BACKSPACE)
3,空格:element.send_keys(Keys.SPACE)
4,tab:element.send_keys(Keys.TAB)
5,退出:element.send_keys(Keys.ESC)
6,回车:element.send_keys(Keys.ENTER)
7,全选:element.send_keys(Keys.CONTROL,"a")
8,复制:element.send_keys(Keys.CONTROL,"c")
9,粘贴:element.send_keys(Keys.CONTROL,"v")
10,剪切:element.send_keys(Keys.CONTROL,"x")
11,上传文件:element.send_keys("文件路径")
12,获取上传的文件名:element.get_attribute("value")
所有keys选项
ADD
= '\ue025'
ALT
= '\ue00a'
ARROW_DOWN
= '\ue015'
ARROW_LEFT
= '\ue012'
ARROW_RIGHT
= '\ue014'
ARROW_UP
= '\ue013'
BACKSPACE
= '\ue003'
BACK_SPACE
= '\ue003'
CANCEL
= '\ue001'
CLEAR
= '\ue005'
COMMAND
= '\ue03d'
CONTROL
= '\ue009'
DECIMAL
= '\ue028'
DELETE
= '\ue017'
DIVIDE
= '\ue029'
DOWN
= '\ue015'
END
= '\ue010'
ENTER
= '\ue007'
EQUALS
= '\ue019'
ESCAPE
= '\ue00c'
F1
= '\ue031'
F10
= '\ue03a'
F11
= '\ue03b'
F12
= '\ue03c'
F2
= '\ue032'
F3
= '\ue033'
F4
= '\ue034'
F5
= '\ue035'
F6
= '\ue036'
F7
= '\ue037'
F8
= '\ue038'
F9
= '\ue039'
HELP
= '\ue002'
HOME
= '\ue011'
INSERT
= '\ue016'
LEFT
= '\ue012'
LEFT_ALT
= '\ue00a'
LEFT_CONTROL
= '\ue009'
LEFT_SHIFT
= '\ue008'
META
= '\ue03d'
MULTIPLY
= '\ue024'
NULL
= '\ue000'
NUMPAD0
= '\ue01a'
NUMPAD1
= '\ue01b'
NUMPAD2
= '\ue01c'
NUMPAD3
= '\ue01d'
NUMPAD4
= '\ue01e'
NUMPAD5
= '\ue01f'
NUMPAD6
= '\ue020'
NUMPAD7
= '\ue021'
NUMPAD8
= '\ue022'
NUMPAD9
= '\ue023'
PAGE_DOWN
= '\ue00f'
PAGE_UP
= '\ue00e'
PAUSE
= '\ue00b'
RETURN
= '\ue006'
RIGHT
= '\ue014'
SEMICOLON
= '\ue018'
SEPARATOR
= '\ue026'
SHIFT
= '\ue008'
SPACE
= '\ue00d'
SUBTRACT
= '\ue027'
TAB
= '\ue004'
UP
= '\ue013'
element.size 元素大小
element.text # 元素文本信息
element.get_attribute(属性名) # 获取属性值
element.is_displayed() # 判断元素是否可见
element.is_enabled() # 判断元素是否可用
element.is_selected() # 判断元素是否选中
# 浏览器操作
driver.back() # 后退页面
driver.forword() # 前进页面
driver.refresh() # 刷新页面
最大化浏览器:driver.maxmize_window()
设置浏览器窗口大小:driver.set_window_size(x,y)
设置浏览器窗口位置driver.set_window_position(x,y)
driver.current_url
driver.quit() # 关闭浏览器窗口,并且关闭server
driver.fullscreen_window() # 截屏
driver.get_cookies() # 所有cookie
driver.get_cookie(cookie_name) # 获取指定的cookie值
driver.delete_all_cookies()
driver.delete_cookie('my_cookie')
driver.get_log(log_type) # 根据指定的类型获取日志
driver.get_log('browser')
driver.get_log('driver')
driver.get_log('client')
driver.get_log('server')
driver.get_screenshot_as_base64()
driver.get_screenshot_as_file('/Screenshots/foo.png')
driver.get_screenshot_as_png()
driver.get_window_position(windowHandle='current') # 获取指定窗口的x,y位置
driver.get_window_size()
driver.orientation # 获取当前设备的方向
driver.name # 获取浏览器名称
driver.page_source # 当前页面的源码
driver.set_window_size(800,600) # 设置窗口大小
driver.set_window_position(0,0)
# 切换
element = driver.switch_to.active_element
alert = driver.switch_to.alert
driver.switch_to.default_content()
driver.switch_to.frame('frame_name')
driver.switch_to.frame(1)
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
driver.switch_to.parent_frame()
driver.switch_to.window('main')
driver.window_handles
driver.current_window_handle
driver.title
driver.current_url
1,隐式等待(作用于全局元素)
deiver.implicitily_wait(timeout)
2, 强制等待
time.sleep(timeout)
3,显式等待(作用于单个元素,并能够查找元素)
from selenium.webdriver.support.wait import WebDriverWait
# 类:WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
# 方法:unitl(method, message='') # 直到返回值不是False
# 方法:until_not(method, message='') # 直到返回值是False
# 例如:
element = WebDriverWait(driver, timeout, poll_frequency).until(lambda x:x.find_element_by("By.XPATH, '//*[@text=value]'"))
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\
until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())
from selenium.webdriver.remote.command import Comand
command = Comand()
driver.execute(command.GET_ALL_SESSIONS) # 执行命令
调用JS操作滚动条
driver.excute_script("window.scrollTo(x,y)")
# 滑动到底部
driver.excute_script("window.scrollTo(0,document.body.scrollHeight)")
ACCEPT_ALERT
= 'acceptAlert'
ADD_COOKIE
= 'addCookie'
CLEAR_APP_CACHE
= 'clearAppCache'
CLEAR_ELEMENT
= 'clearElement'
CLEAR_LOCAL_STORAGE
= 'clearLocalStorage'
CLEAR_SESSION_STORAGE
= 'clearSessionStorage'
CLICK
= 'mouseClick'
CLICK_ELEMENT
= 'clickElement'
CLOSE
= 'close'
CONTEXT_HANDLES
= 'getContextHandles'
CURRENT_CONTEXT_HANDLE
= 'getCurrentContextHandle'
DELETE_ALL_COOKIES
= 'deleteAllCookies'
DELETE_COOKIE
= 'deleteCookie'
DELETE_SESSION
= 'deleteSession'
DISMISS_ALERT
= 'dismissAlert'
DOUBLE_CLICK
= 'mouseDoubleClick'
DOUBLE_TAP
= 'touchDoubleTap'
ELEMENT_SCREENSHOT
= 'elementScreenshot'
EXECUTE_ASYNC_SCRIPT
= 'executeAsyncScript'
EXECUTE_SCRIPT
= 'executeScript'
EXECUTE_SQL
= 'executeSql'
FIND_CHILD_ELEMENT
= 'findChildElement'
FIND_CHILD_ELEMENTS
= 'findChildElements'
FIND_ELEMENT
= 'findElement'
FIND_ELEMENTS
= 'findElements'
FLICK
= 'touchFlick'
FULLSCREEN_WINDOW
= 'fullscreenWindow'
GET
= 'get'
GET_ACTIVE_ELEMENT
= 'getActiveElement'
GET_ALERT_TEXT
= 'getAlertText'
GET_ALL_COOKIES
= 'getCookies'
GET_ALL_SESSIONS
= 'getAllSessions'
GET_APP_CACHE
= 'getAppCache'
GET_APP_CACHE_STATUS
= 'getAppCacheStatus'
GET_AVAILABLE_LOG_TYPES
= 'getAvailableLogTypes'
GET_COOKIE
= 'getCookie'
GET_CURRENT_URL
= 'getCurrentUrl'
GET_CURRENT_WINDOW_HANDLE
= 'getCurrentWindowHandle'
GET_ELEMENT_ARIA_LABEL
= 'getElementAriaLabel'
GET_ELEMENT_ARIA_ROLE
= 'getElementAriaRole'
GET_ELEMENT_ATTRIBUTE
= 'getElementAttribute'
GET_ELEMENT_LOCATION
= 'getElementLocation'
GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW
= 'getElementLocationOnceScrolledIntoView'
GET_ELEMENT_PROPERTY
= 'getElementProperty'
GET_ELEMENT_RECT
= 'getElementRect'
GET_ELEMENT_SIZE
= 'getElementSize'
GET_ELEMENT_TAG_NAME
= 'getElementTagName'
GET_ELEMENT_TEXT
= 'getElementText'
GET_ELEMENT_VALUE
= 'getElementValue'
GET_ELEMENT_VALUE_OF_CSS_PROPERTY
= 'getElementValueOfCssProperty'
GET_LOCAL_STORAGE_ITEM
= 'getLocalStorageItem'
GET_LOCAL_STORAGE_KEYS
= 'getLocalStorageKeys'
GET_LOCAL_STORAGE_SIZE
= 'getLocalStorageSize'
GET_LOCATION
= 'getLocation'
GET_LOG
= 'getLog'
GET_NETWORK_CONNECTION
= 'getNetworkConnection'
GET_PAGE_SOURCE
= 'getPageSource'
GET_SCREEN_ORIENTATION
= 'getScreenOrientation'
GET_SESSION_STORAGE_ITEM
= 'getSessionStorageItem'
GET_SESSION_STORAGE_KEYS
= 'getSessionStorageKeys'
GET_SESSION_STORAGE_SIZE
= 'getSessionStorageSize'
GET_TIMEOUTS
= 'getTimeouts'
GET_TITLE
= 'getTitle'
GET_WINDOW_HANDLES
= 'getWindowHandles'
GET_WINDOW_POSITION
= 'getWindowPosition'
GET_WINDOW_RECT
= 'getWindowRect'
GET_WINDOW_SIZE
= 'getWindowSize'
GO_BACK
= 'goBack'
GO_FORWARD
= 'goForward'
IMPLICIT_WAIT
= 'implicitlyWait'
IS_ELEMENT_DISPLAYED
= 'isElementDisplayed'
IS_ELEMENT_ENABLED
= 'isElementEnabled'
IS_ELEMENT_SELECTED
= 'isElementSelected'
LONG_PRESS
= 'touchLongPress'
MAXIMIZE_WINDOW
= 'windowMaximize'
MINIMIZE_WINDOW
= 'minimizeWindow'
MOUSE_DOWN
= 'mouseButtonDown'
MOUSE_UP
= 'mouseButtonUp'
MOVE_TO
= 'mouseMoveTo'
NEW_SESSION
= 'newSession'
NEW_WINDOW
= 'newWindow'
PRINT_PAGE
= 'printPage'
QUIT
= 'quit'
REFRESH
= 'refresh'
REMOVE_LOCAL_STORAGE_ITEM
= 'removeLocalStorageItem'
REMOVE_SESSION_STORAGE_ITEM
= 'removeSessionStorageItem'
SCREENSHOT
= 'screenshot'
SEND_KEYS_TO_ACTIVE_ELEMENT
= 'sendKeysToActiveElement'
SEND_KEYS_TO_ELEMENT
= 'sendKeysToElement'
SET_ALERT_CREDENTIALS
= 'setAlertCredentials'
SET_ALERT_VALUE
= 'setAlertValue'
SET_ELEMENT_SELECTED
= 'setElementSelected'
SET_LOCAL_STORAGE_ITEM
= 'setLocalStorageItem'
SET_LOCATION
= 'setLocation'
SET_NETWORK_CONNECTION
= 'setNetworkConnection'
SET_SCREEN_ORIENTATION
= 'setScreenOrientation'
SET_SCRIPT_TIMEOUT
= 'setScriptTimeout'
SET_SESSION_STORAGE_ITEM
= 'setSessionStorageItem'
SET_TIMEOUTS
= 'setTimeouts'
SET_WINDOW_POSITION
= 'setWindowPosition'
SET_WINDOW_RECT
= 'setWindowRect'
SET_WINDOW_SIZE
= 'setWindowSize'
SINGLE_TAP
= 'touchSingleTap'
STATUS
= 'status'
SUBMIT_ELEMENT
= 'submitElement'
SWITCH_TO_CONTEXT
= 'switchToContext'
SWITCH_TO_FRAME
= 'switchToFrame'
SWITCH_TO_PARENT_FRAME
= 'switchToParentFrame'
SWITCH_TO_WINDOW
= 'switchToWindow'
TOUCH_DOWN
= 'touchDown'
TOUCH_MOVE
= 'touchMove'
TOUCH_SCROLL
= 'touchScroll'
TOUCH_UP
= 'touchUp'
UPLOAD_FILE
= 'uploadFile'
W3C_ACCEPT_ALERT
= 'w3cAcceptAlert'
W3C_ACTIONS
= 'actions'
W3C_CLEAR_ACTIONS
= 'clearActionState'
W3C_DISMISS_ALERT
= 'w3cDismissAlert'
W3C_EXECUTE_SCRIPT
= 'w3cExecuteScript'
W3C_EXECUTE_SCRIPT_ASYNC
= 'w3cExecuteScriptAsync'
W3C_GET_ACTIVE_ELEMENT
= 'w3cGetActiveElement'
W3C_GET_ALERT_TEXT
= 'w3cGetAlertText'
W3C_GET_CURRENT_WINDOW_HANDLE
= 'w3cGetCurrentWindowHandle'
W3C_GET_WINDOW_HANDLES
= 'w3cGetWindowHandles'
W3C_GET_WINDOW_POSITION
= 'w3cGetWindowPosition'
W3C_GET_WINDOW_SIZE
= 'w3cGetWindowSize'
W3C_MAXIMIZE_WINDOW
= 'w3cMaximizeWindow'
W3C_SET_ALERT_VALUE
= 'w3cSetAlertValue'
W3C_SET_WINDOW_POSITION
= 'w3cSetWindowPosition'
W3C_SET_WINDOW_SIZE
= 'w3cSetWindowSize'
from selenium.webdriver.remote.errorhandler import ErrorHandler
errorhandler = ErrorHandler()
errorhandler.check_response(response) # 检查webderiver返回的json格式的结果是否有错误
# 错误码
from selenium.webdriver.remote.errorhandler import ErrorCode
errorcode = ErrorCode()
errorcode.ELEMENT_NOT_VISIBLE # 元素不可见
......
ELEMENT_CLICK_INTERCEPTED
= [64, 'element click intercepted']
ELEMENT_IS_NOT_SELECTABLE
= [15, 'element not selectable']
ELEMENT_NOT_INTERACTABLE
= [60, 'element not interactable']
ELEMENT_NOT_VISIBLE
= [11, 'element not visible']
IME_ENGINE_ACTIVATION_FAILED
= [31, 'ime engine activation failed']
IME_NOT_AVAILABLE
= [30, 'ime not available']
INSECURE_CERTIFICATE
= ['insecure certificate']
INVALID_ARGUMENT
= [61, 'invalid argument']
INVALID_COOKIE_DOMAIN
= [24, 'invalid cookie domain']
INVALID_COORDINATES
= ['invalid coordinates']
INVALID_ELEMENT_COORDINATES
= [29, 'invalid element coordinates']
INVALID_ELEMENT_STATE
= [12, 'invalid element state']
INVALID_SELECTOR
= [32, 'invalid selector']
INVALID_SESSION_ID
= ['invalid session id']
INVALID_XPATH_SELECTOR
= [51, 'invalid selector']
INVALID_XPATH_SELECTOR_RETURN_TYPER
= [52, 'invalid selector']
JAVASCRIPT_ERROR
= [17, 'javascript error']
METHOD_NOT_ALLOWED
= [405, 'unsupported operation']
MOVE_TARGET_OUT_OF_BOUNDS
= [34, 'move target out of bounds']
NO_ALERT_OPEN
= [27, 'no such alert']
NO_SUCH_COOKIE
= [62, 'no such cookie']
NO_SUCH_ELEMENT
= [7, 'no such element']
NO_SUCH_FRAME
= [8, 'no such frame']
NO_SUCH_WINDOW
= [23, 'no such window']
SCRIPT_TIMEOUT
= [28, 'script timeout']
SESSION_NOT_CREATED
= [33, 'session not created']
STALE_ELEMENT_REFERENCE
= [10, 'stale element reference']
SUCCESS
= 0
TIMEOUT
= [21, 'timeout']
UNABLE_TO_CAPTURE_SCREEN
= [63, 'unable to capture screen']
UNABLE_TO_SET_COOKIE
= [25, 'unable to set cookie']
UNEXPECTED_ALERT_OPEN
= [26, 'unexpected alert open']
UNKNOWN_COMMAND
= [9, 'unknown command']
UNKNOWN_ERROR
= [13, 'unknown error']
UNKNOWN_METHOD
= ['unknown method exception']
XPATH_LOOKUP_ERROR
= [19, 'invalid selector']
ALL_NETWORK = ConnectionType(6) # 所有网络模式
WIFI_NETWORK = ConnectionType(2) # wifi模式
DATA_NETWORK = ConnectionType(4) # 移动数据模式
AIRPLANE_MODE = ConnectionType(1) # fixing模式
# 设置网路模式
driver.mobile.se_network_connection(deriver.mobile.WIFI_NETWORK)
driver.mobile.se_network_connection(deriver.mobile.ALL_NETWORK)
driver.mobile.se_network_connection(deriver.mobile.DATA_NETWORK)
driver.mobile.se_network_connection(deriver.mobile.AIRPLANE_MODE) # 飞行模式
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element(By.TAG_NAME, "select"))
select.options() # 返回所有可选的数据列表
select.all_selected_options() # 返回所有选中的数据列表
select.first_selected_option() # 返回当前选中的选项中第一个选项
select.select_by_value(value) # 根据选项的value值选中元素
select.select_by_index(index) # 根据下表选中元素
select.select_by_visible_text(text) # 根据文案选择元素
select.deselect_all() # 取消全选
select.deselect_by_value(value) # 取消某个value值对应的选择
select.deselect_by_index(index) # 取消某个下标值对应的选择
select .deselect_by_visible_text(text) # 取消某个文案对应的选择
from selenium.webdriver.support.abstract_event_listener import
AbstractEventListener
after_change_value_of
(element, driver)[source]
after_click
(element, driver)[source]
after_close
(driver)[source]
after_execute_script
(script, driver)[source]
after_find
(by, value, driver)[source]
after_navigate_back
(driver)[source]
after_navigate_forward
(driver)[source]
after_navigate_to
(url, driver)[source]
after_quit
(driver)[source]
before_change_value_of
(element, driver)[source]
before_click
(element, driver)[source]
before_close
(driver)[source]
before_execute_script
(script, driver)[source]
before_find
(by, value, driver)[source]
before_navigate_back
(driver)[source]
before_navigate_forward
(driver)[source]
before_navigate_to
(url, driver)[source]
before_quit
(driver)[source]
on_exception
(exception, driver)[source]¶