Home > Back-end >  Perform mouse actions in Selenium Python
Perform mouse actions in Selenium Python

Time:03-25

I have the following script:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

url = 'https://www.icribis.com/it/'

codes = [...] # my codes

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)

time.sleep(0.5)

# Disable cookies
driver.execute_script('return document.querySelector("#usercentrics-root").shadowRoot.querySelector("#uc-center-container > div:nth-child(2) div > button:nth-child(3)")').click()

time.sleep(0.5)

for code in codes:
    # Select Codice fiscale (= fiscal code)
    wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='search-type-fiscal-code']"))).click()
    time.sleep(0.5)

    # Clean the search bar
    driver.find_element(by=By.ID, value='companySearchFormInput').clear()
    time.sleep(0.5)

    # Insert the fiscal code in the search bar
    wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='search']"))).send_keys(code)
    time.sleep(0.5)

    # Click on the button
    wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@id='companySearch']//input[@type='submit']"))).click()
    time.sleep(0.5)

    # Rest of the code

driver.close()

Is there a way in Selenium Python to do some of the above operations using the mouse?

For example, in the case of # Select Codice fiscale (= fiscal code), move the mouse up to go over the word "Codice fiscale" (in any point of it) and then click (select) it?

Thanks in advance for your clarifications.

CodePudding user response:

Yes, it is possible to move the mouse up to go over the element "Codice fiscale", but that would involve more lines of code and may induce unwanted complexity.

Instead using expected_conditions like visibility_of_element_located() and element_to_be_clickable() can make your job much easier and simpler.


Actions API

Actions API is the low-level interface for providing virtualised device input to the web browser. Unlike the high-level element interactions, which conducts additional validations, the Actions API provides granular control over input devices. Selenium provides access to 3 input sources as follows:

  • Keyboard actions: A representation of any key input device for interacting with a web page.
  • Mouse actions: A representation of any pointer device for interacting with a web page.
  • Scroll wheel actions: A representation of a scroll wheel input device for interacting with a web page.

Mouse actions APIs

A few examples of Mouse actions are as follows:

  • Click and hold: It will move to the element and clicks (without releasing) in the middle of the given element.

  • Context click: This method firstly performs a mouse-move to the location of the element and performs the context-click (right click) on the given element.

  • Double click: It will move to the element and performs a double-click in the middle of the given element.

  • Move to element: This method moves the mouse to the middle of the element. The element is also scrolled into the view on performing this action..

    from selenium import webdriver
    driver = webdriver.Chrome()
    
    # Navigate to url
    driver.get("http://www.google.com")
    
    # Store 'google search' button web element
    gmailLink = driver.find_element(By.LINK_TEXT, "Gmail")
    
    # Performs mouse move action onto the element
    webdriver.ActionChains(driver).move_to_element(gmailLink).perform()
    
  • Move by offset: This method moves the mouse from its current position (or 0,0) by the given offset. If the coordinates are outside the view window, then the mouse will end up outside the browser window.

    from selenium import webdriver
    driver = webdriver.Chrome()
    
    # Navigate to url
    driver.get("http://www.google.com")
    
    # Store 'google search' button web element
    gmailLink = driver.find_element(By.LINK_TEXT, "Gmail")
    # Set x and y offset positions of element
    xOffset = 100
    yOffset = 100
    # Performs mouse move action onto the element
    webdriver.ActionChains(driver).move_by_offset(xOffset,yOffset).perform()
    
  • dragAndDrop: This method firstly performs a click-and-hold on the source element, moves to the location of the target element and then releases the mouse.

  • dragAndDropBy: This method firstly performs a click-and-hold on the source element, moves to the given offset and then releases the mouse.

  • release: This action releases the depressed left mouse button. If WebElement is passed, it will release depressed left mouse button on the given WebElement

  • Related