你需要有一定的selenium基础
包导入
from selenium.webdriver import ActionChains
调用方式一:链式调用
ActionChains(driver).动作1.动作2.perform()
调用方式二:分步执行
myaction = ActionChains(driver)
myaction.动作1
myaction.动作2
myaction.perform()
鼠标方法
参数
说明
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
移动到元素(依据偏移量)
pause
seconds
暂停
perform★★★
无参数
执行
release★
on_element=None
释放
reset_actions
无参数
重置动作
scroll
x: int, y: int, delta_x: int, delta_y: int, duration: int = 0, origin: str = "viewport"
滚动(废弃)
scroll_by_amount
delta_x: int, delta_y: int
通过给定的偏差滚动
scroll_from_origin
scroll_origin: ScrollOrigin, delta_x: int, delta_y: int
通过给定的原始位置+偏差滚动
scroll_to_element
element
滚动到元素
send_keys★
*keys_to_send
发送按键
send_keys_to_element★
element, *keys_to_send
发送按键到元素
from selenium.webdriver import ActionChains
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://sahitest.com/demo/clicks.htm')
ele_click = driver.find_element('css selector', "input[value='click me']")
ele_dblclick = driver.find_element('css selector', "input[value='dbl click me']")
ele_rightclick = driver.find_element('css selector', "input[value='right click me']")
ActionChains(driver).click(ele_click).perform()
ActionChains(driver).double_click(ele_dblclick).perform()
ActionChains(driver).context_click(ele_rightclick).perform()
from selenium.webdriver import ActionChains
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://106.14.1.150:8090/forum.php')
ele_username = driver.find_element('css selector', "#ls_username")
ele_username.send_keys('admin')
ActionChains(driver).double_click(ele_username).perform()
from time import sleep
from selenium.webdriver import ActionChains
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://sahitest.com/demo/dragDropMooTools.htm')
ele_drag = driver.find_element('css selector', '#dragger')
ele_items = driver.find_elements('css selector', '.item')
for ele_item in ele_items:
ActionChains(driver).drag_and_drop(ele_drag, ele_item).perform()
sleep(1)
from time import sleep
from selenium.webdriver import ActionChains
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://sahitest.com/demo/dragDropMooTools.htm')
ele_drag = driver.find_element('css selector', '#dragger')
ele_items = driver.find_elements('css selector', '.item')
for ele_item in ele_items:
myaction = ActionChains(driver)
myaction.click_and_hold(ele_drag) # 点击并按住
myaction.release(ele_item) # 松开按键
myaction.perform()
sleep(1)
from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
driver = webdriver.Chrome()
driver.get('http://106.14.1.150:8090/forum.php')
# 1. 鼠标移动到快捷导航元素
ele_navi = driver.find_element('css selector','#qmenu')
ActionChains(driver).move_to_element(ele_navi).perform()
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
driver.get('http://106.14.1.150:8090/forum.php')
ele_username = driver.find_element('css selector','#ls_username')
x= ele_username.location['x']
y= ele_username.location['y']
ActionChains(driver).move_by_offset(xoffset=x,yoffset=y).click().perform()
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
driver.maximize_window()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})""",
}) #让浏览器感知不到是自动化软件在动作
driver.get('http://vip.ytesting.com/loginController.do?login')
driver.find_element('css selector','#userName').send_keys('松勤VIP账号')
driver.find_element('css selector','#password').send_keys('松勤VIP账号的密码')
ele_huakuai = driver.find_element('css selector','#nc_1_n1z')
ele_gundongtiao = driver.find_element('css selector','#nc_1__scale_text')
distance = ele_gundongtiao.rect['width'] - ele_huakuai.rect['width']
ActionChains(driver).drag_and_drop_by_offset(ele_huakuai,xoffset=distance,yoffset=0).perform()
sleep(2)
driver.find_element('css selector','#but_login').click()
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
driver.get('http://106.14.1.150:8090/forum.php')
ele_username = driver.find_element('id','ls_username')
ActionChains(driver).send_keys_to_element(ele_username,'ad').perform()
ActionChains(driver).click(ele_username).send_keys('mi').perform()
ActionChains(driver).click(ele_username).key_down('n').key_up('n').perform()
手机扫一扫
移动阅读更方便
你可能感兴趣的文章