JRoller JRoller - 3 months ago 14
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?

EDIT:

I've found the solution for this issue:

driver.execute_script("arguments[0].scrollIntoView(true);", element)
time.sleep(0.5)
element.click()


The time.sleep was the missing link.

Answer

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)