click at at an arbitrary position in web browser with selenium 2 (Python binding) click at at an arbitrary position in web browser with selenium 2 (Python binding) selenium selenium

click at at an arbitrary position in web browser with selenium 2 (Python binding)


Assuming there is nothing else going on on your page that would interfere with the click, this should do it:

homeLink = driver.find_element_by_link_text("Home")homeLink.click() #clicking on the Home button and mouse cursor should? stay hereprint homeLink.size, homeLink.locationhelpLink = driver.find_element_by_link_text("Help")print helpLink.size, helpLink.locationaction = webdriver.common.action_chains.ActionChains(driver)action.move_to_element_with_offset(homeLink, 150, 0) #move 150 pixels to the right to access Help linkaction.click()action.perform()

You should use move_to_element_with_offset if you want the mouse position to be relative to an element. Otherwise, move_by_offset moves the mouse relative to the previous mouse position. When you using click or move_to_element, the mouse is placed in the center of the element. (The Java documentation about these is explicit.)


Hard to say for you exact situation but I know a workaround to the question in the summary and bold

send the left-click in a web browser window in a specific point

You just use execute_script and do the clicking using javascript.

self.driver.execute_script('el = document.elementFromPoint(47, 457); el.click();')

It's convenient in a pinch because you can debug find the coordinates of an element in a browser by opening console and using querySelector (works the same as Webdriver's By.CSS_SELECTOR):

el = document.querySelector('div > h1');var boundaries = el.getBoundingClientRect();console.log(boundaries.top, boundaries.right, boundaries.bottom, boundaries.left);

That being said, it's a really bad idea to write your tests to click a specific point. But I have found that sometimes even when el.click() or an action chain aren't working, execute script will still work using a querySelector which is about as good as what you would be doing in Selenium.

self.driver.execute_script('el = document.querySelector("div > h1"); el.click();')