Mouse Click & Keyboard Event using ActionChains
ActionChains are a way to automate low level
interactions such as mouse movements, mouse button actions, key press, and
context menu interactions.
Selenium has the built-in ability to handle various
types of keyboard and mouse events. This is useful for doing more complex
actions like hover over and drag and drop.
You need to import action_chains for mouse and keyboard event methods, below is the
import statement.
from selenium.webdriver.common.action_chains import
ActionChains.
When you call methods for actions on the
ActionChains object, the actions are stored in a queue in the ActionChains
object.
When you call perform(), the events are fired in the
order they are queued up.
To create an object ‘action‘ of Selenium Actionchains:
actions = ActionChains(driver)
To focus on element using WebDriver:
actions.move_to_element(webelement)
perform() method is used to execute the action.
actions.move_to_element(webelement).perform()
To click on the element:
Ex: actions.move_to_element(webelement).click().perform()
Click() method is used here to click the element.
Keyboard
Events Using Selenium Actionchains
key_down(value, element=None) :Sends a key press only, without releasing it. Should only be
used with modifier keys (Control, Alt and Shift).
value - The
modifier key to send. Values are defined in Keys class.
element -The
element to send keys. If None, sends a key to current focused element.
Ex: ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').perform()
key_up(value, element=None) : Releases a modifier key.
value - The modifier key to send. Values are defined
in Keys class.
element -The element to send keys. If None, sends a
key to current focused element.
Ex: ActionChains(driver).key_up(Keys.CONTROL).perform()
send_keys(charsequence) : sends a series of keystrokes to current focused
element.
charsequence - any string value representing the sequence of
keystrokes to be sent
Ex: ActionChains(driver).send_keys('c').perform()
send_keys_to_element(Element, charsequence) :sends a series of keystrokes to an element.
element - element that will receive the keystrokes, usually a
text field
charsequence - any string value representing the
sequence of keystrokes to be sent
Ex: ActionChains(driver).send_keys(webelement,'c').perform()
Mouse
Events Using Selenium Actionchains
Click(element=None):
it is used to click the element.
element: The element to click. If None,
clicks on current mouse position.
Ex: ActionChains(driver).move_to_element(webelement).click().perform()
double_click(element=None):
Performs a double-click at the current mouse location.
element: The element to double_click. If
None, double clicks on current mouse position.
Ex: ActionChains(driver).move_to_element(webelement).double_click().perform()
click_and_hold(element=None):Holds
down the left mouse button on an element.(without releasing)
element: The element to mouse down. If
None, clicks on current mouse position.
Ex: ActionChains(driver).move_to_element(webelement).click_and_hold(element).perform()
context_click(element=None):Performs a context-click (right
click) on an element.
element: The element to context click.
If None, clicks on current mouse position.
Ex: ActionChains(driver).move_to_element(webelement).context_click().perform()
drag_and_drop(source, target):Holds
down the left mouse button on the source element,then moves to the target
element and releases the mouse button.
source: The element to mouse down.
target: The element to mouse up.
Ex: ActionChains(driver).move_to_element(webelement).drag_and_drop(sEle,tEle).perform()
drag_and_drop_by_offset(source, xoffset, yoffset):Holds down the left mouse
button on the source element, then moves to the target offset and releases the
mouse button.
source - The element to mouse down
xOffset – to shift horizontally,
yOffset – to shift vertically
Ex:
ActionChains(driver).move_to_element(webelement).drag_and_drop(sEle,200,250).perform()
move_by_offset(xoffset, yoffset):Moving the mouse to an offset
from current mouse position.
xoffset: X offset to move to, as a positive or
negative integer.
yoffset: Y offset to move to, as a positive or
negative integer.
Ex: ActionChains(driver).move_by_offset(250,300).click().perform()
move_to_element(to_element):Moving the mouse to the middle of an
element.
to_element: The WebElement to move to.
Ex: ActionChains(driver).move_to_element(webelement).Click().perform()
move_to_element_with_offset(to_element, xoffset, yoffset):Move the mouse by an
offset of the specified element.Offsets are relative to the top-left corner of
the element.
to_element: The WebElement to move to.
xoffset: X offset to move to.
yoffset: Y offset to move to.
Ex: ActionChains(driver).move_to_element(webelement,250,300).Click().perform()
release(on_element=None) :Releasing a held mouse button on
an element.
on_element: The element to mouse up. If None,
releases on current mouse position.
Ex: ActionChains(driver).move_to_element(webelement).release().perform()
Few
more actionchains methods
pause(seconds):
Pause all inputs for the specified duration in
seconds
perform()
Performs all stored actions.
reset_actions()
Clears actions that are already stored locally and
on the remote end.
Below
are the scenarios how to use actionchains methods in selenium.
Scenario 1:
Open Chrome browser.
Navigate to the https://www.goindigo.in application.
moving the mouse over an element on web page
displays Book menu
Click on Book a flight.
Print the title of the page.
Script:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.goindigo.in")
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.goindigo.in")
# Book menu id
bookid=driver.find_element_by_xpath("//a[@title='Book']")
bookid=driver.find_element_by_xpath("//a[@title='Book']")
# create action
object for action_chains
actions=ActionChains(driver)
actions=ActionChains(driver)
# using move to element for move the mouse course to book element
actions.move_to_element(bookid)
# perform the
action
actions.perform()
driver.find_element_by_xpath("//div[@class='nav-inner-items']//a[contains(@href,'flight-booking_header')]").click()
pageTitle=driver.title
print("Page title is : ",pageTitle)
actions.perform()
driver.find_element_by_xpath("//div[@class='nav-inner-items']//a[contains(@href,'flight-booking_header')]").click()
pageTitle=driver.title
print("Page title is : ",pageTitle)
Scenario 2:
Open Chrome browser.
Navigate to the https://www.facebook.com application.
I am trying to set value in the first Name and Surname fields, but I
am passing lower case letter in send_keys with the keypress of Shift, but it I
will enter as a capital letters.
Script:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.facebook.com/")
firstname=driver.find_element_by_xpath("//input[@name='firstname']")
lastname=driver.find_element_by_xpath("//input[@name='lastname']")
# create an object for action_chains
actions=ActionChains(driver)
lastname=driver.find_element_by_xpath("//input[@name='lastname']")
# create an object for action_chains
actions=ActionChains(driver)
# for FirstName
field
actions.key_down(Keys.SHIFT,firstname)
actions.send_keys("hello")
actions.key_up(Keys.SHIFT,firstname)
actions.key_down(Keys.SHIFT,firstname)
actions.send_keys("hello")
actions.key_up(Keys.SHIFT,firstname)
#for surname
Field
actions.key_down(Keys.SHIFT,lastname)
actions.send_keys("hello")
actions.key_up(Keys.SHIFT,lastname)
actions.key_down(Keys.SHIFT,lastname)
actions.send_keys("hello")
actions.key_up(Keys.SHIFT,lastname)
Scenario 3: To Scroll Web page using Actions Class in Selenium
Open Chrome browser.
Navigate to the https://www.python.org application.
Do Scroll down
Do Scroll Up.
Note
: We are using PageDown and
PageUp keyword option as a parameters to
send_keys.
Script:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.python.org/")
# create an object
actions=ActionChains(driver)
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.python.org/")
# create an object
actions=ActionChains(driver)
actions.send_keys(Keys.PAGE_DOWN)
actions.perform()
time.sleep(3)
actions.perform()
time.sleep(3)
# Reset the
actions
actions.reset_actions()
actions.reset_actions()
actions.send_keys(Keys.PAGE_UP)
actions.perform()
actions.perform()
How To Perform Double Click
Action In Selenium
Scenario 4:
Open Chrome browser.
Double click on the box.it will be available in the
bottom.
Script:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://api.jquery.com/dblclick/")
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://api.jquery.com/dblclick/")
driver.switch_to.frame(0)
xpaths1=driver.find_element_by_xpath("/html/body/div")
actions=ActionChains(driver)
xpaths1=driver.find_element_by_xpath("/html/body/div")
actions=ActionChains(driver)
# double click on
the element
actions.double_click(xpaths1)
actions.perform()
time.sleep(2)
actions.double_click(xpaths1)
actions.perform()
time.sleep(2)
xpaths2=driver.find_element_by_xpath("//div[@class='dbl']")
actions.reset_actions()
actions.double_click(xpaths2)
actions.perform()
actions.reset_actions()
actions.double_click(xpaths2)
actions.perform()
Drag And Drop Using ActionChains in selenium:
Scenario 5:
Open Chrome browser.
Drag and Drop.
Note: we are using drag_and_drop method for drag and
drop for below script.
Script
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://jqueryui.com/droppable/")
frmobj=driver.find_element(By.XPATH,"//*[@class='demo-frame']")
actions=ActionChains(driver)
driver.switch_to.frame(frmobj)
sEle=driver.find_element(By.CSS_SELECTOR,"div#draggable")
tEle=driver.find_element(By.CSS_SELECTOR,"div#droppable")
actions.drag_and_drop(sEle,tEle)
actions.perform()
actions=ActionChains(driver)
driver.switch_to.frame(frmobj)
sEle=driver.find_element(By.CSS_SELECTOR,"div#draggable")
tEle=driver.find_element(By.CSS_SELECTOR,"div#droppable")
actions.drag_and_drop(sEle,tEle)
actions.perform()
Scenario 6:
Open Chrome browser.
Drag and Drop.(with out using drag_and_drop method)
Note: we are using Click_and_hold and release
methods.
Script
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://jqueryui.com/droppable/")
frmobj=driver.find_element(By.XPATH,"//*[@class='demo-frame']")
actions=ActionChains(driver)
driver.switch_to.frame(frmobj)
sEle=driver.find_element(By.CSS_SELECTOR,"div#draggable")
tEle=driver.find_element(By.CSS_SELECTOR,"div#droppable")
actions.click_and_hold(sEle)
actions.move_to_element(tEle)
actions.release()
actions.perform()
actions=ActionChains(driver)
driver.switch_to.frame(frmobj)
sEle=driver.find_element(By.CSS_SELECTOR,"div#draggable")
tEle=driver.find_element(By.CSS_SELECTOR,"div#droppable")
actions.click_and_hold(sEle)
actions.move_to_element(tEle)
actions.release()
actions.perform()
Comments
Post a Comment