When we want to use Mouse or Keyboard actions we should
create an "ActionChains" class object and we should import
"from selenium.webdriver import ActionChains".
- Syntax:
- from selenium.webdriver import ActionChains
- #
- action_obj = ActionChains(driver)
Method for ActionChains Class:
perform(): perform-function in
the ActionChains is used to create a chain of actions or
operations you want to perform.
move_to_element(element): This function is used to Move the mouse point to an element.
- Syntax:
- a = ActionChains(driver)
- element = driver.find_element(By.ID, "123")
- a.move_to_element(element).perform()
double_click(element): This function can perform a double-click on an element.
- Syntax:
- a = ActionChains(driver)
- element = driver.find_element(By.ID, "123")
- a.double_click(element).perform()
context_click(element): This function can perform a (Mouse)Right-Click operation on an
element.
- Syntax:
- a = ActionChains(driver)
- element = driver.find_element(By.ID, "123")
- a.context_click(element).perform()
click_and_hold(element): This
function can perform hold down the left mouse button on an element.
- Syntax:
- a = ActionChains(driver)
- element = driver.find_element(By.ID, "123")
- a.click_and_hold("element").peroform()
drag_and_drop(Source, Destination):
This function
- Syntax:
- a = ActionChains(driver)
- source = driver.find_element(By.ID, "123")
- destination = driver.find_element(By.ID, "ABC")
- a.drag_and_drop(source, destination).perform()
Note: if the element = None, then it performs the action on the
current mouse position.
Program:
- from selenium import webdriver
- from selenium.webdriver import ActionChains
- from selenium.webdriver.common.by import By
- driver = webdriver.Chrome()
- driver.get("https://testingcolleges.blogspot.com/2023/08/alert-demo-pages.html")
- action = ActionChains(driver)
- # Double-Click
- double = driver.find_element(By.ID, "doubleclick")
- action.move_to_element(double).double_click().perform()
- time.sleep(2)
- # Right-Click
- right = driver.find_element(By.ID, "rclick")
- action.context_click(right).perform()
- time.sleep(2)
- driver.close()
OUTPUT:
No comments:
Post a Comment