Sunday, June 28, 2020

Selenium Python Tutorial 8 | Python Selenium Mouse Click | Mouse Action Chains | PyAutoGUI Mouse

This is the next tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains Python Selenium mouse actions. First view the Selenium Python Mouse Click Tutorial. Then read on. 
 
Here is my first Selenium Python example. I have put explanations in the comments within the code (lines starting with #).

# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
from selenium.webdriver import ActionChains as A

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL1 = "https://inderpsingh.blogspot.com/2014/08/demowebapp_24.html"
URL2 = "https://crossbrowsertesting.github.io/drag-and-drop"
heading_css_locator = ".post-body > div:nth-child(1) > div:nth-child(1) > form:nth-child(1) > h3:nth-child(1)"
distance_id_locator = "distance"
draggable_id_locator = "draggable"
droppable_id_locator = "droppable"
wait_time_out =15

# Define the Selenium WebDriver variable.
driver = webdriver.Firefox(executable_path=exec_path)
# Define the variable for Explicit Wait.
wait_variable = W(driver, wait_time_out)
# Navigate to URL1.
driver.get(URL1)
# Find the heading web element, on which to perform the mouse double click.
heading_element = wait_variable.until(E.presence_of_element_located((By.CSS_SELECTOR, heading_css_locator)))
distance_element = wait_variable.until(E.presence_of_element_located((By.ID, distance_id_locator)))
# Define the variable of type ActionChains for Mouse Action Chains.
a = A(driver)
# Selenium Python Mouse Double Click on heading web element
a.double_click(heading_element)
# Call one of the multiple Selenium Python Mouse Move methods.
# Note: Selenium Python Mouse move methods implement Selenium Python Mouse Hover too.
a.move_to_element_with_offset(distance_element, 0, 0)
# Selenium Python Mouse Click
a.click_and_hold(distance_element)
a.release()
# Type 1000 at the mouse pointer location i.e. the beginning of the distance web element.
a.send_keys("1000")
# Call ActionChains perform method to run all the methods queued thus far.
a.perform()

# Navigate to URL2.
driver.get(URL2)
draggable_element = wait_variable.until(E.presence_of_element_located((By.ID, draggable_id_locator)))
droppable_element = wait_variable.until(E.presence_of_element_located((By.ID, droppable_id_locator)))
# Define another variable of type ActionChains. 
b = A(driver)
# Python Selenium Mouse Drag and Drop
b.drag_and_drop(draggable_element, droppable_element)
# Right click the Mouse on the draggable web element.
b.context_click(draggable_element)
b.perform()

A reliable way to operate the mouse with Selenium WebDriver is to use the PyAutoGUI library. You install the PyAutoGUI package with the pip command. Then install the PyAutoGUI package in the project. I explained such tasks here. Here is my second Selenium Python example for Selenium Python Mouse Actions using PyAutoGUI.

# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
import pyautogui as P
import time

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://inderpsingh.blogspot.com/2014/08/demowebapp_24.html"
distance_id_locator = "distance"
speed_id_locator = "speed"
wait_time_out = 15
driver = webdriver.Firefox(executable_path=exec_path)
wait_variable = W(driver, wait_time_out)
driver.get(URL)
driver.execute_script("window.scrollBy(0,120)","")
distance_element = wait_variable.until(E.presence_of_element_located((By.ID, distance_id_locator)))
distance_element.send_keys("1000")
speed_element = wait_variable.until(E.presence_of_element_located((By.ID, speed_id_locator)))
speed_element.send_keys("50")
# When this script is running, move the mouse pointer to the Calculate Travel Days button.
time.sleep(5)
x, y = P.position()
# Print the x and y coordinates of the Calculate Travel Days button.
print ("X is ", str(x), "Y is ", str(y))
# Give the x and y coordinates printed by the print statement above e.g. 222, 573. The 3rd argument (optional) is the time the mouse pointer should take to move to the x and y coordinates.
# Note: After locating the x, y coordinates by running this Python script once, you may comment out the above lines from time.sleep(5) to print ("X is ", str(x), "Y is ", str(y))
# PyAutoGUI mouse operations
P.moveTo(222, 573, 3)
# Python Selenium Mouse Click
P.leftClick()

Want to learn more details? Want to see the above Selenium Python examples working? Then, please view my Selenium Python Mouse Move and Mouse Click Tutorial. Thank you.

No comments:

Post a Comment