Python Selenium `move_by_offset` doesn't work Python Selenium `move_by_offset` doesn't work selenium selenium

Python Selenium `move_by_offset` doesn't work


Try to wait some seconds after moving mouse. For example, the following code to get screen captures in my CentOS7.3 host worked for me.

chrome_options = webdriver.ChromeOptions()chrome_options.add_argument('--headless')chrome_options.add_argument("--window-size=720,480")chrome_options.add_argument('--no-sandbox')driver = webdriver.Chrome('/usr/bin/chromedriver', chrome_options=chrome_options, service_args=['--verbose', '--log-path=/tmp/chromedriver.log'])driver.get(url)time.sleep(6)ActionChains(driver).move_by_offset(50, 50).perform()time.sleep(2)filename="/tmp/Screenshots/uuid.png"driver.save_screenshot(filename)


It seems move_by_offset can not scroll a page, but it still can move the mouse to an offset from current mouse position.

To confirm we can try to do this:

driver = webdriver.Chrome()driver.get('https://www.wikipedia.org/')actions = ActionChains(driver)actions.move_by_offset(300, 500).context_click().perform()

To scroll a page by offset we have to use js:

driver = webdriver.Chrome()driver.get('https://www.wikipedia.org/')driver.execute_script('window.scrollBy(0, 500)')  # x=0, y=500