selenium Python 总结一些工作中可能会经常使用到的API。
- 1.获取当前页面的Url
方法:current_url 实例:driver.current_url
- 1
- 2
- 1
- 2
- 2.获取元素坐标
-
方法:location
-
解释:首先查找到你要获取元素的,然后调用location方法
-
实例:driver.find_element_by_xpath(
"xpath").location
- 1
- 2
- 3
- 1
- 2
- 3
- 3.表单的提交
-
方法:submit
-
解释:查找到表单(
from)直接调用submit即可
-
实例:driver.find_element_by_id(
"form1").submit()
- 1
- 2
- 3
- 1
- 2
- 3
- 4.获取CSS的属性值
-
方法:value_of_css_property(css_name)
-
实例:driver.find_element_by_css_selector(
"input.btn").value_of_css_property(
"input.btn")
- 1
- 2
- 1
- 2
- 5.获取元素的属性值
-
方法:get_attribute(element_name)
-
实例:driver.find_element_by_id(
"kw").get_attribute(
"kw")
- 1
- 2
- 1
- 2
- 6.判断元素是否被选中
-
方法:is_selected()
-
实例:driver.find_element_by_id(
"form1").is_selected()
- 1
- 2
- 1
- 2
- 7.返回元素的大小
-
方法:size
-
实例:driver.find_element_by_id(
"iptPassword").size
-
返回值:{
'width':
250,
'height':
30}
- 1
- 2
- 3
- 1
- 2
- 3
- 8.判断元素是否显示
-
方法:is_displayed()
-
实例:driver.find_element_by_id(
"iptPassword").is_displayed()
- 1
- 2
- 1
- 2
- 9.判断元素是否被使用
-
方法:is_enabled()
-
实例:driver.find_element_by_id(
"iptPassword").is_enabled()
- 1
- 2
- 1
- 2
- 10.获取元素的文本值
-
方法:text
-
实例:driver.find_element_by_id(
"iptUsername").text
- 1
- 2
- 1
- 2
- 11.元素赋值
-
方法:send_keys(*values)
-
实例:driver.find_element_by_id(
"iptUsername").send_keys(
'admin')
- 1
- 2
- 1
- 2
- 12.返回元素的tagName
-
方法:tag_name
-
实例:driver.find_element_by_id(
"iptUsername").tag_name
- 1
- 2
- 1
- 2
- 13.删除浏览器所有的cookies
方法:delete_all_cookies() 实例:driver.delete_all_cookies()
- 1
- 2
- 1
- 2
- 14.删除指定的cookie
-
方法:delete_cookie(name)
-
实例:deriver.delete_cookie(
"my_cookie_name")
- 1
- 2
- 1
- 2
- 15.关闭浏览器
方法:close() 实例:driver.close()
- 1
- 2
- 1
- 2
- 16.关闭浏览器并且退出驱动程序
方法:quit() 实例:driver.quit()
- 1
- 2
- 1
- 2
- 17.返回上一页
方法:back() 实例:driver.back()
- 1
- 2
- 1
- 2
- 18.清空输入框
方法:clear() 实例:driver.clear()
- 1
- 2
- 1
- 2
- 19.浏览器窗口最大化
方法:maximize_window() 实例:driver.maximize_window()
- 1
- 2
- 1
- 2
- 20.查看浏览器的名字
方法:name 实例:drvier.name
- 1
- 2
- 1
- 2
- 21.返回当前会话中的cookies
方法:get_cookies() 实例:driver.get_cookies()
- 1
- 2
- 1
- 2
- 22.根据cookie name 查找映射Value值
-
方法:driver.get_cookie(cookie_name)
-
实例:driver.get_cookie(
"NET_SessionId")
- 1
- 2
- 1
- 2
- 23.截取当前页面
-
方法:get_screenshot_as_file(filename)
-
实例:driver.get_screenshot_as_file(
"D:\Program Files\Python27\NM.bmp")
- 1
- 2
- 1
- 2
- 24.获取当前窗口的坐标
方法:get_window_position() 实例:driver.get_window_position()
- 1
- 2
- 1
- 2
- 25.获取当前窗口的长和宽
方法:get_window_size() 实例:driver.get_window_size()
- 1
- 2
- 1
- 2
ActionChains类鼠标操作的常用方法:
引入ActionChains类:from selenium.webdriver.common.action_chains import ActionChains
- 26.右击
-
方法:context_click()
-
实例:ActionChains(driver).context_click(driver.find_element_by_id(
"id")).perform()
- 1
- 2
- 1
- 2
- 27.双击
-
方法:double_click()
-
实例:ActionChains(driver).double_click(driver.find_element_by_name(
"name")).perform()
- 1
- 2
- 1
- 2
- 28:鼠标拖放
-
方法:drag_and_drop(source, target)
-
source:鼠标按下的源元素;target:鼠标释放的目标元素
-
实例:element = driver.find_element_by_name(
"name")
-
target = driver.find_element_by_name(
"name")
-
ActionChains(driver).drag_and_drop(element, target).perform()
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
- 29:鼠标悬停在一个元素上(hover)
-
方法:move_to_element()
-
实例:above = driver.find_element_by_xpath(
"xpath路径")
-
ActionChains(driver).move_to_element(above).perform()
- 1
- 2
- 3
- 1
- 2
- 3
- 30:按下鼠标左键在一个元素上
-
方法:click_and_hold()
-
实例:left = driver.find_element_by_name(
"name")
-
ActionChains(driver).click_and_hold(left).perform()
- 1
- 2
- 3
- 1
- 2
- 3
键盘事件:
引入Keys类包:from selenium.webdriver.common.keys import Keys
- 31:输入
-
方法:send_keys()
-
实例:driver.find_element_by_id(
"id").send_keys(
"XXX")
- 1
- 2
- 1
- 2
- 32:输入空格
-
方法:send_keys(Keys.SPACE)
-
实例:driver.find_element_by_id(
"id").send_keys(Keys.SPACE)
- 1
- 2
- 1
- 2
- 33:ctrl + a
全选输入框的内容
ctrl + x
剪切输入框的内容
ctrl + v
粘贴到输入框
ctrl + c
复制
-
方法:send_keys(Keys.CONTROL,
'a')
-
实例:driver.find_element_by_id(
"id").send_keys(Keys.CONTROL,
'a')
- 1
- 2
- 1
- 2
- 34:回车代替点击
-
方法:send_keys(Keys.ENTER)
-
实例:driver.find_element_by_id(
"id").send_keys(Keys.ENTER)
- 1
- 2
- 1
- 2
- 35:制表键(Tab)
-
方法:send_keys(Keys.TAB)
-
实例:driver.find_element_by_id(
"id").send_keys(Keys.TAB)
- 1
- 2
- 1
- 2
- 36:回退键(Esc)
-
方法:send_keys(Keys.ESCAPE)
-
实例:driver.find_element_by_id(
"id").send_keys(Keys.ESCAPE)
- 1
- 2
- 1
- 2
等待时间
导入 WebDriverWait 包
from selenium.webdriver.support.ui import WebDriverWait
导入 time 包
import time
- 37:固定等待时间
-
方法:sleep()
-
实例:time.sleep(
5)
# 等待5秒
- 1
- 2
- 1
- 2
- 38:等待一个元素被发现,或一个命令完成,超出了设置时间则抛出异常智能等待。
-
方法:implicitly_wait()
-
实例:driver.implicitly_wait(
30)
- 1
- 2
- 1
- 2
- 39:在设置时间内,默认每隔一段时间检测一次当前页面元素是否存在,如果超过设置时间检测不到则抛出异常
-
"方法:WebDriverWait()"
-
-
#WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
-
#——driver:WebDriver的驱动程序(Ie, Firefox, Chrome或远程)
-
#——timeout:最长超时时间,默认以秒为单位
-
#——poll_frequency:休眠时间的间隔(步长)时间,默认为0.5秒
-
#——ignored_exceptions:超时后的异常信息,默认情况下抛NoSuchElementException异常
-
-
-
实例:
-
element = WebDriverWait(driver,
10).until(
lambda x: x.find_element_by_id(
"id"))
-
-
-
#一般由unit()或until_not()方法配合使用,同上:
-
-
-
调用该方法提供的驱动程序作为一个参数,直到返回值不为
False。
-
——until(method, message=’’)
-
-
调用该方法提供的驱动程序作为一个参数,直到返回值为
False。
-
——until_not(method, message=’’)
-
-
-
# 还可以与expected_conditions 一起使用
-
from selenium.webdriver.support
import expected_conditions
as EC
-
-
# 实例: 判断某个元素是否可见并且是enable的,这样才clickable
-
-
WebDriverWait(dr,
15,
1).until(EC.element_to_be_clickable((By.ID,
"EmployeeListMenu")),
"Not Find element")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 40:选择当前页面上所有tag
name为input的元素 inputs = driver.find_elements_by_tag_name(‘input‘)
- 1
- 2
- 1
- 2
- 41:从中过滤出type为checkbox的元素,并勾选上
-
for input
in inputs:
-
if input.get_attribute(‘type‘) == ‘checkbox‘:
-
input.click()
- 1
- 2
- 3
- 1
- 2
- 3
- 42:使用CSS定位选择所有type为checkbox的元素,并勾选上
-
checkboxes = driver.find_elements_by_css_selector(‘input[type = checkbox]‘)
-
for checkbox
in checkboxes:
-
checkbox.click()
- 1
- 2
- 3
- 1
- 2
- 3
- 43:把最后一个checkbox的勾去掉,pop()方法空参数时,默认移除list中的最后一个元素。
driver.find_elements_by_css_selector(‘input[type = checkbox]‘).pop().click()
- 1
- 1
切换活动对象
- 44:切换浏览器handle
-
# 切换不同的tab页
-
方法:driver.switch_to.window(window_name)
-
# 备注:从A页跳转到B页,句柄已经切换过去,但是焦点没有切过去,所以需要switch_to.window,把焦点也切过去,才可以在当前页进行操作。
-
# 切换是思路,获取所有的句柄,因为返回是一个list,而且要切换的对象都是最后一个,可以使用[-1]直接切过去
-
# 例如:
-
driver.switch_to.window(driver.window_handles[
-
1])
- 1
- 2
- 3
- 4
- 5
- 6
- 45:返回上一级表单
-
方法: driver.switch_to_parent_content()
#旧方法
-
driver.switch_to.parent_content
#新方法
- 1
- 2
- 1
- 2
- 46:返回最外层表单
-
方法: driver.switch_to_default_content()
#旧方法
-
driver.switch_to.default_content()
#新方法
- 1
- 2
- 1
- 2
- 47:切换到指定frame中
-
方法:driver.switch_to.frame(
'xxx')
-
实例:driver.switch_to.frame(
'frame_name')
-
driver.switch_to.frame(index)
-
driver.switch_to.frame(driver.find_elements_by_tag_name(
"iframe")[
0])
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 48:获取当前page的title
方法:driver.title 实例:driver.title
- 1
- 2
- 3
- 1
- 2
- 3
- 49:焦点切换到弹窗。
方法:driver.switch_to_alert() 实例:driver.switch_to_alert()
- 1
- 2
- 3
- 1
- 2
- 3
- 50:前进
方法: driver.forward()
- 1
- 2
- 1
- 2
- 51:刷新页面
方法:driver.refresh()
- 1
- 1