pythonseleniummouse-position

Python Selenium x and y offset of element to click on


in Selenium with Python I want to mouse-navigate to elements by coordinates and to click etc. - because that xpath-way and others doesn't work.

I saw that there is an action "move_by_offset" for it.

Where do I could read out the X and Y position of an exist element?

I tried this for exampel:

driver.get("https://www.google.de")
driver.implicitly_wait(10)
driver.maximize_window()
##Cookieaccept
search1 = driver.find_element(By.ID, "L2AGLb")
search1.click()

##google logo
element = driver.find_element(By.CLASS_NAME, "lnXdpd")

Is there a function? Because

loc = element.location
print (loc) 

Gives X and Y output

Gives my an output of x and y but I need both in separte integers to use move_by_offset()

action = ActionChains(driver)
action.move_by_offset(X,Y)

Solution

  • Since element.location return a dict with x and y coordinates, use dictionary.get() method

    loc = element.location
    X, Y = loc.get('x'), loc.get('y')
    action = ActionChains(driver)
    action.move_by_offset(X,Y)
    

    Otherwise you can use ActionChain.move_to_element() method

    element = driver.find_element(By.CLASS_NAME, "lnXdpd")
    action = ActionChains(driver)
    action.move_to_element(element).perform()