JRoller JRoller - 1 year ago 82
Python Question

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.

Answer Source

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)