How to fix “Element is not clickable” in Selenium WebDriver tests?

Three ways to resolve the “Element is not clickable” issue in Selenium.

  1. The element is not clickable, such as disabled or not allowed to click by nature (e.g. a hidden field)
    this is easy to find out by inspecting (via right-click in Chrome), the solution is to change/refine the locator.
  2. The element is not viewable in the browser (by the definition of Selenium)
The test failed (the above: webpage, the below test failure output)

Analyse

The test step:

driver.find_element(:class, "fa-warning").click
Failure/Error: driver.find_element(:class, "fa-warning").click
Selenium::WebDriver::Error::ElementClickInterceptedError:
element click intercepted: Element is not clickable at point (1201, 611)
(Session info: chrome=102.0.5005.115)
  1. Make the browser window bigger (not recommended).
    Depending on the page content and browser size, it might work in some cases.
  2. Scroll the webpage down further to make the element fully visible.
    My recommendation, see below.

Solution

There are several approaches to scrolling in Selenium.

  1. Scroll based on the element’s X,Y coordinate
elem = driver.find_element(:class, "fa-warning")
elem_pos = elem.location.y
driver.execute_script("window.scroll(0, #{elem_pos + 100})")
sleep 0.5 # add delay to allow JS execution
elem.click
  • Find the element
  • Get the Y position of the element in the browser
  • Invoke JavaScript to scroll down a further 100 pixels
  • Add a small delay to allow JS scrolling to complete
  • Click the element (now fully visible)
elem = driver.find_element(:class, "fa-warning")
driver.execute_script("arguments[0].scrollIntoView(true);", elem);
sleep 0.5
elem.click
// to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
// to top
driver.execute_script("window.scrollTo(0, 0);")
elem = driver.find_element(:class, "fa-warning")
driver.action.scroll_by(100, 200).perform
elem.click
scroll_to(ELEM)
scroll_by(DELTA_X, DELTA_Y)
scroll_from(SCROLL_ORIGIN, DELTA_X, DELTA_Y)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store