Selenium系列(4)- web控件交互

常用方法

点击

driver.find_element_by_css_selector("#su").click()

输入/清空内容

ele_input = driver.find_element_by_css_selector("#kw")
# 输入
ele_input.send_keys("selenium测试")
# 清空
ele_input.clear()

获取元素尺寸、坐标

ele_input = driver.find_element_by_css_selector("#kw")
# 获取元素尺寸(宽、高)
size = ele_input.size
# 获取元素坐标(x,y)
location = ele_input.location

🍘 ATTENTION:

  • 元素坐标值是从元素左上角和浏览器内容区域左上角来定的
  • 返回的都是字典类型

获取元素文本

btn = driver.find_element_by_css_selector("#su")
# 获取元素文本信息
btn_text = btn.text

获取元素属性值

link_tieba = driver.find_element_by_css_selector("#s-top-left > a:nth-child(6)")
# 获取属性值href
link_tieba.get_attribute("href")

检查元素是否可见、是否可点击、是否被选择等

# 是否可见
element.is_displayed()
# 是否可点击
element.is_enabled()
# 是否被选中
element.is_selected()

🍘 ATTENTION:

  • 元素有disabled属性,则元素不可点击
  • 元素有selected属性,则元素已被选择
  • 元素有displayed:none属性,则元素不可见

表单提交

element.submit()

🍘 ATTENTION:

  • submit()相当于表单填写完成之后,回车->提交功能

ActionChains

原理:调用ActionChains的方法是,不会立即执行,而是将所有的操作,按顺序存放在一个队列里,当调用perform()方法时,队列中的事件会依次执行

基本用法:

  • 生成一个动作action=ActionChains(driver)
  • 动作添加方法1 action.方法1
  • 动作添加方法2 action.方法2
  • 调用perform()方法执行(action.perform())

单击、双击、右键

# http://sahitest.com/demo/clicks.htm
ele_click=driver.find_element_by_xpath("//input[@value='click me']") 
ele_doubleclick=driver.find_element_by_xpath("//input[@value='dbl click me']")
ele_rightclick=driver.find_element_by_xpath("//input[@value='right click me']")
action = ActionChains(driver)
# 单击
action.click(ele_click)
# 右键
action.context_click(ele_rightclick)
# 双击
action.double_click(ele_doubleclick)
# 执行方法
action.perform()

移动光标

# 以百度页面为例,将鼠标移动到设置上
ele = self.driver.find_element_by_link_text("设置")
action = ActionChains(self.driver)
# 移动鼠标
action.move_to_element(ele)
action.perform()

拖拽元素

# http://sahitest.com/demo/dragDropMooTools.htm
drag_ele = self.driver.find_element_by_id("dragger")
drop_ele = self.driver.find_element_by_xpath("/html/body/div[2]")
action = ActionChains(self.driver)
# 将元素drag_ele拖拽到drag_ele上
action.drag_and_drop(drag_ele,drop_ele).perform()
# 或者click_and_hold和release(点击并且不放,拖动到另一个元素上再释放)
action.click_and_hold(drag_ele).release(drop_ele)
action.perform()

模拟按键方法

# http://sahitest.com/demo/label.htm
ele = self.driver.find_element_by_xpath("/html/body/label[1]/input")
action = ActionChains(self.driver)
ele.click()
# 输入username
action.send_keys("username").pause(1)
# 输入空格键
action.send_keys(Keys.SPACE).pause(1)
# 输入删除键
action.send_kesy(Keys.BACK_SPACE).pause(1)
action.perform

基本上所有的按键都可以用ActionChains模拟出来

小结:常用方法列表

click(on_element=None) ——单击鼠标左键
click_and_hold(on_element=None) ——点击鼠标左键,不松开
context_click(on_element=None) ——点击鼠标右键
double_click(on_element=None) ——双击鼠标左键
drag_and_drop(source, target) ——拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开
key_down(value, element=None) ——按下某个键盘上的键
key_up(value, element=None) ——松开某个键
move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标
move_to_element(to_element) ——鼠标移动到某个元素
move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置
perform() ——执行链中的所有动作
release(on_element=None) ——在某个元素位置松开鼠标左键
send_keys(*keys_to_send) ——发送某个键到当前焦点的元素
send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素

TouchAction

TouchAction类似于ActionChains,两者的区别在于:

  • ActionChains是针对PC端程序鼠标的一系列操作,对H5页面操作无效
  • TouchAction可以对H5页面进行操作——点击、拖拽、多点触控、模拟手势等的各种操作

常见方法

tap 						# 在指定元素上敲击
double_tap 					#在指定元素上双敲击
tap_and_hold 				#在指定元素上点击但不释放
move						#手势移动指定偏移(未释放)
release						# 释放手势
scroll 						# 手势点击并滚动
scroll_from_element			# 从某个元素位置开始手势点击并滚动(向下滑动为负数,向上滑动为正数)
long_press					# 长按元素
flick						# 手势滑动
flick_element				# 从某个元素位置开始手势滑动(向上滑动为负数,向下滑动为整数
perform						# 执行

更多方法可以参考官网

以百度页面为栗

"""
打开百度页面,输入selenium测试
点击搜索按钮
滑动到页面底部
点击下一页按钮
"""
self.driver.get("https://www.baidu.com/")
ele = self.driver.find_element_by_id("kw")
ele_search = self.driver.find_element_by_id("su")
ele.send_keys("selenium测试")
action = TouchActions(self.driver)
action.tap(ele_search)
action.perform()
action.scroll_from_element(ele,0,10000).perform()

问题解决

  • 问题描述:"unknown command: Cannot call non W3C standard command while in W3C mode"

  • 解决方法:在启动浏览器时进入W3C即可

def setup(self):
    option = webdriver.ChromeOptions()
    option.add_experimental_option('w3c',False)
    self.driver = webdriver.Chrome(options=option)
    self.driver.implicitly_wait(5)
    self.driver.maximize_window()
posted @ 2021-10-10 21:37  莫伊101  阅读(44)  评论(0编辑  收藏  举报