Scroll operation

move_to_element() Method:

This method is used to move the mouse to the middle of an element.It takes webelement as an argument.

Perform() method helps to do the operation of move_to_element() Method.

	Syntax: actionchain_object.move_to_element(webelement).perform()

Example:

  • Import the ActionChains Class
  • Launch the webpage "http://www.dummypoint.com/seleniumtemplate.html"
  • Enter the details in contact form using send_key() method.
  • Now using the move_to_element() method we need to move to the required element when it is not visible to the screen.
  • Click on the submit button after moving to it by using move_to_element() method.
  • Wait for 5 sec.
  • Close the Web Browser


Scroll_Operation.py

from selenium import webdriver
import time
from selenium.common.exceptions import ElementNotVisibleException, NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import  expected_conditions as ec

driver = webdriver.Chrome("/Users/admin/Documents/Skill2Lead/Others/drivers/chromedriver.exe")
#driver = webdriver.Firefox(executable_path="/Users/sujithreddy/Documents/Code2Lead/Others/drivers/geckodriver")

driver.get("http://www.dummypoint.com/seleniumtemplate.html")
assert "Selenium Template" in driver.title
time.sleep(2)

wait = WebDriverWait(driver,25,poll_frequency=1,ignored_exceptions=[ElementNotVisibleException,NoSuchElementException])
wait.until(ec.presence_of_element_located((By.LINK_TEXT,"Form"))).click()
wait.until(ec.presence_of_element_located((By.ID,"reused_form")))
time.sleep(2)

wait.until(ec.presence_of_element_located((By.ID,"name"))).send_keys("Skill2Lead")
wait.until(ec.presence_of_element_located((By.ID,"email"))).send_keys("abc@gmail.com")
wait.until(ec.presence_of_element_located((By.ID,"message"))).send_keys("ABCDEFG")
captcha = wait.until(ec.presence_of_element_located((By.ID,"captcha_image")))
wait.until(ec.presence_of_element_located((By.ID,"captcha"))).send_keys(captcha.text)

postButton = wait.until(ec.presence_of_element_located((By.ID,"btnContactUs")))

# Scroll Gesture
actions = ActionChains(driver)
actions.move_to_element(postButton).perform()
postButton.click()

time.sleep(5)
driver.quit()