move_by_offset() is an ActionChains method in Selenium Python that moves the mouse cursor by a specified X and Y offset from its current position, useful for relative and coordinate-based mouse actions.
This example shows how to move the mouse cursor 200 pixels right and 200 pixels down from its current position.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("https://www.geeksforgeeks.org/")
actions = ActionChains(driver)
actions.move_by_offset(200, 200).perform()
Output

Explanation:
- webdriver.Firefox() launches the Firefox browser for automation.
- driver.get(...) opens the GeeksforGeeks website.
- ActionChains(driver) creates an object to perform mouse and keyboard actions.
- move_by_offset(200, 200) moves the mouse cursor 200 pixels right and 200 pixels down from its current position.
- perform() executes the mouse movement action.
Syntax
move_by_offset(xoffset, yoffset)
Parameters:
- xoffset: Horizontal movement (positive = right, negative = left)
- yoffset: Vertical movement (positive = down, negative = up)
Examples
Example 1: This example moves the mouse 100 pixels right and 50 pixels down after opening a webpage.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
d = webdriver.Firefox()
d.get("https://www.geeksforgeeks.org/")
a = ActionChains(d)
a.move_by_offset(100, 50).perform()
Output

Explanation: move_by_offset(100, 50) shifts the mouse right and down from its current location.
Example 2: This example demonstrates moving the mouse left and upward using negative offsets.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
d = webdriver.Firefox()
d.get("https://www.geeksforgeeks.org/")
a = ActionChains(d)
a.move_by_offset(-80, -40).perform()
Output

Explanation: Negative values in move_by_offset(-80, -40) move the mouse opposite to the positive direction.
Example 3: This example performs multiple mouse movements using chained offsets.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
d = webdriver.Firefox()
d.get("https://www.geeksforgeeks.org/")
a = ActionChains(d)
a.move_by_offset(100, 0).move_by_offset(0, 100).perform()
Output

Explanation: Each move_by_offset() is executed sequentially, creating a step-by-step mouse movement.