01. Intro

Selenium Webdriver can control mouse action such as 'mouse hover', 'drog and drop' and slider.

 

02. Mouse Hover

You can use 'move_to_element()' method to hover the mouse on the element.

 

The first step is to import the ActionChains. ActionChains in Selenium is ActionChains is a way to automate low level interactions such as mouse movements.

from selenium.webdriver import ActionChains

 

The second step is to declare an actions driver.

actions = ActionChains(self.driver)

 

Use the action.move_to_element() method to move to the element and perform() method to execute.

actions.move_to_element(hoverElement).perform()

 

▶ Testcase

# tc2. Find the mouse hover btn
hoverElement = self.driver.find_element(By.ID, 'mousehover')
refresh = self.driver.find_element(By.XPATH, ".//div[@class='mouse-hover-content']//a[text()='Reload']")

# tc3. Hover the mouse on the btn
actions = ActionChains(self.driver)
actions.move_to_element(hoverElement).perform()

# tc4. Click the dropdown menu
actions.move_to_element(refresh).click().perform()

 

 

03. Drag and Drop

You can use the 'drag_and_drop' method to drag and drop the element.

There are two ways to drag and drop.

The first way is to use drag_and_drop' method.

The second way is to click the element and move to the destination and release.

# The first way
actions.drag_and_drop(fromElement, toElement).perform()

# The second way
actions.click_and_hold(fromElement).move_to_element(toElement).release().perform()

 

▶ Testcase

fromElement = self.driver.find_element(By.ID, 'draggable')
toElement = self.driver.find_element(By.ID, 'droppable')

actions = ActionChains(self.driver)
actions.drag_and_drop(fromElement, toElement).perform()

 

 

04. Slider

Sometimes we should move the element of the slider bar using a mouse.

To this, you can use the drag_and_drop_by_offset(self, source, xoffset, yoffset) method.

 

▶ Testcase

actions = ActionChains(self.driver)
actions.drag_and_drop_by_offset(element, 100, 0).perform()

 

 

+ Recent posts