Selenium webdriver with Python on chrome – Scroll to the exact middle of an element


I’m trying to click on an element solely by getting it with XPATH. I get an exception that the element is un-clickable in the given location.

I know for sure that the center of the element is clickable, so how do i get the exact middle (x,y) of the element and click it with Selenium using Python?


I’ve found the solution for this issue:

driver.execute_script("arguments[0].scrollIntoView(true);", element)

The time.sleep was the missing link.

Asked By: Shizzle



Actually selenium itself try to click on element at center position of element, so this exception normally occurs when target element overlayed by other element due to size of the window or any other reason, like it would be hidden inside scroll bar etc.

So basically if you want to get exact element into view port, so you could click on it, you should try using scrollIntoView() method which scrolls the current element into the visible area of the browser window as below :-

element = driver.find_element..
driver.execute_script("arguments[0].scrollIntoView()", element)
Answered By: Saurabh Gaur

The below solution is working for me, (Python)

element = driver.find_element_by_xpath("//*[text()='Installer Package ']")
driver.execute_script("arguments[0].scrollIntoView(**{block: 'center', inline: 'nearest'}**)", element))

For JavaScript:

WebElement e = driver.findElement("txt"));
JavascriptExecutor j = (JavascriptExecutor)driver;
j.executeScript ("arguments[0].scrollIntoView(**{block: 'center', inline: 'nearest'}**)", e);

{block: ‘center’, inline: ‘nearest’}

above mentioned is the main argument here, to perfectly locate element at the middle (Note: sometimes element will be located at botttom, but will not completely present in screen. so click action cannot be performed. But passing this argument will avoid those problems)

Answered By: Mano Surya