from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# clickAndHold
with webdriver.Chrome() as driver:
print('打开百度')
driver.get("https://www.baidu.com")
# Store 'google search' button web element
print('选中链接并存储')
searchLink = driver.find_element(By.LINK_TEXT, "地图")
# Perform click-and-hold action on the element
print('鼠标移动到该元素,然后在给定元素的中间单击(不释放).')
webdriver.ActionChains(driver).click_and_hold(searchLink).perform()
print('看到地图变色')
time.sleep(10)
print('会话结束,关闭浏览器')
# contextClick
with webdriver.Chrome() as driver:
print('打开百度')
driver.get("https://www.baidu.com")
# Store 'google search' button web element
print('选中链接并存储')
searchLink = driver.find_element(By.LINK_TEXT, "地图")
# Perform click-and-hold action on the element
print('将鼠标移动到元素的位置, 然后在给定元素执行上下文点击(右键单击).')
webdriver.ActionChains(driver).context_click(searchLink).perform()
print('查看效果')
time.sleep(10)
print('会话结束,关闭浏览器')
# doubleClick
with webdriver.Chrome() as driver:
print('打开百度')
driver.get("https://www.baidu.com")
# Store 'google search' button web element
print('选中链接并存储')
searchLink = driver.find_element(By.LINK_TEXT, "地图")
# Perform click-and-hold action on the element
print('移动到该元素, 并在给定元素的中间双击.')
webdriver.ActionChains(driver).double_click(searchLink).perform()
print('查看效果')
time.sleep(10)
print('会话结束,关闭浏览器')
# moveToElement
with webdriver.Chrome() as driver:
print('打开百度')
driver.get("https://www.baidu.com")
# Store 'google search' button web element
print('选中链接并存储')
searchLink = driver.find_element(By.LINK_TEXT, "地图")
# Perform click-and-hold action on the element
print('鼠标移到元素的中间. 执行此操作时, 该元素也会滚动到视图中.')
webdriver.ActionChains(driver).move_to_element(searchLink).perform()
print('查看效果')
time.sleep(10)
print('会话结束,关闭浏览器')
# moveByOffset
with webdriver.Chrome() as driver:
print('打开百度')
driver.get("https://www.baidu.com")
# Store 'google search' button web element
print('选中链接并存储')
searchLink = driver.find_element(By.LINK_TEXT, "地图")
xOffset = 100
yOffset = 100
# Perform click-and-hold action on the element
print('将鼠标从其当前位置(或0,0)移动给定的偏移量')
webdriver.ActionChains(driver).move_by_offset(xOffset, yOffset).perform()
print('查看效果')
time.sleep(10)
print('会话结束,关闭浏览器')
# dragAndDrop
with webdriver.Chrome() as driver:
print('打开拖拽示例网址')
driver.get("https://crossbrowsertesting.github.io/drag-and-drop")
# Store 'box A' as source element
sourceEle = driver.find_element(By.ID, "draggable")
# Store 'box B' as source element
targetEle = driver.find_element(By.ID, "droppable")
# Performs drag and drop action of sourceEle onto the targetEle
print('首先在源元素上单击并按住,然后移动到目标元素的位置后释放鼠标')
webdriver.ActionChains(driver).drag_and_drop(sourceEle, targetEle).perform()
print('查看效果')
time.sleep(10)
print('会话结束,关闭浏览器')
# dragAndDropBy
with webdriver.Chrome() as driver:
print('打开拖拽示例网址')
driver.get("https://crossbrowsertesting.github.io/drag-and-drop")
# Store 'box A' as source element
sourceEle = driver.find_element(By.ID, "draggable")
# Store 'box B' as source element
targetEle = driver.find_element(By.ID, "droppable")
targetEleXOffset = targetEle.location.get("x")
targetEleYOffset = targetEle.location.get("y")
print('首先在源元素上单击并按住, 移至给定的偏移量后释放鼠标')
# Performs dragAndDropBy onto the target element offset position
webdriver.ActionChains(driver).drag_and_drop_by_offset(
sourceEle, targetEleXOffset, targetEleYOffset).perform()
print('查看效果')
time.sleep(10)
print('会话结束,关闭浏览器')
# release
with webdriver.Chrome() as driver:
print('打开拖拽示例网址')
driver.get("https://crossbrowsertesting.github.io/drag-and-drop")
# Store 'box A' as source element
sourceEle = driver.find_element(By.ID, "draggable")
# Store 'box B' as source element
targetEle = driver.find_element(By.ID, "droppable")
print('释放按下的鼠标左键. 如果WebElement转移了, 它将释放给定WebElement上按下的鼠标左键.')
# Performs dragAndDropBy onto the target element offset position
webdriver.ActionChains(driver).click_and_hold(sourceEle).move_to_element(targetEle).perform()
# Performs release event
webdriver.ActionChains(driver).release().perform()
print('查看效果')
time.sleep(10)
print('会话结束,关闭浏览器')