欢迎光临
我们一直在努力

selenium第三阶段

鼠标操作

需要导入ActionChains()类,传入driver驱动,添加相关的动作,perform()去执行

click()

from selenium import webdriver
import time

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 最大化窗口
driver.maximize_window()

# 鼠标相关的操作,创建一个对象,传入driver对象
action = ActionChains(driver)
# 随便定位一个元素
element = driver.find_element('xpath','kw')

# 鼠标单击
action.click(element).perform()
# 鼠标双击
action.double_click(element).perform()
# 右击
action.context_click(element).perform()
# 鼠标移动到指定坐标,offset:补偿,偏移
action.move_by_offset().perform()
# 鼠标移动到指定控件的指定位置
action.move_to_element_with_offset(element, 10, 10).perform()
# 按下鼠标左键不松开
action.click_and_hold(element).perform()
# 拖动元素到某个位置
action.drag_and_drop_by_offset(element,1,1).perform()
# 拖动一个元素到目标元素
action.drag_and_drop(element,element).perform()
# 在元素上按下鼠标左键
action.key_down(element).perform()
# 在元素上松开鼠标左键
action.key_up(element).perform()

# 按下后停滞1秒
action.perform(1000)

# 重置动作
action.release()

driver.quit()

赞(0)
未经允许不得转载:171主机测评 » selenium第三阶段
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址