Is there a way to show current cursor position or something like this? I have action chain that should click on the exact point on some object but I guess I've picked up wrong coordinates. I use Firefox webdriver. Here's how the script looks like:
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("http://www.mysite/")
elem = driver.find_element_by_xpath('//div[@id="player"][1]/object[@type="application/x-shockwave-flash"]')
action_chains = ActionChains(driver)
action_chains.move_to_element_with_offset(elem, 660, 420).perform()
action_chains.click().perform()
time.sleep(10)
driver.close()
I had an opaque HTML5 canvas where I had to check values of tooltips at various offsets, and what trouble! After lots of banging my head, I learned several important things:
move_to_element_by_offset
has issues prior to 2.42. See also https://code.google.com/p/selenium/issues/detail?id=4215
Even after an update, move_to_element_by_offset
still seems persnickety. I switched to a combination of the ActionChains move_to_element
hover (defaults to the center of the element) and a second ActionChains
with move_by_offset
Now that you're in position, re-request the position-sensitive element. In my case, that's a tooltip object. This selenium.webdriver.remote.element
will have the .location['x']
and .location['y']
members you need to sanity check your position.
Things that don't work for grabbing cursor position after a perform
'd ActionChain to hover, to save everyone some time:
.location['x']
and .location['y']
on the element you pass into move_to_element
. These values are not updated by ActionChains
.switch_to_active_element
; I'm not clear what elements qualify as "active", but it doesn't acknowledge our tooltips as active elements and gave me back [0, 0] coordinates.get_window_position
; the documentation was a bit vague, but this does just what the function suggests: a window rather than a cursor position.