在 Selenium 中显示当前光标位置 [英] Show current cursor position in Selenium
问题描述
有没有办法显示当前光标位置或类似的东西?我有动作链应该点击某个对象上的确切点,但我想我选择了错误的坐标.我使用 Firefox 网络驱动程序.脚本如下所示:
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()
推荐答案
我有一个不透明的 HTML5 画布,我必须在其中检查各种偏移量处的工具提示值,多麻烦!在反复敲打我的脑袋之后,我学到了几件重要的事情:
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
在 2.42 之前存在问题.另请参阅 https://code.google.com/p/selenium/issues/detail?id=4215
move_to_element_by_offset
has issues prior to 2.42. See also https://code.google.com/p/selenium/issues/detail?id=4215
即使在更新之后,move_to_element_by_offset
仍然看起来很挑剔.我切换到 ActionChains move_to_element
悬停(默认为元素的中心)和带有 move_by_offset
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
既然您已就位,请重新请求位置敏感元素.就我而言,这是一个工具提示对象.这个 selenium.webdriver.remote.element
将包含您需要的 .location['x']
和 .location['y']
成员理智地检查你的位置.
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.
在 perform
'd ActionChain 悬停后抓取光标位置不起作用的事情,以节省大家一些时间:
Things that don't work for grabbing cursor position after a perform
'd ActionChain to hover, to save everyone some time:
.location['x']
和.location['y']
在您传入move_to_element
的元素上.ActionChains
不会更新这些值.- selenium.webdriver.remote.webdriver
switch_to_active_element
;我不清楚哪些元素符合活动"的条件,但它不会将我们的工具提示确认为活动元素并返回 [0, 0] 坐标. - selenium.webdriver.remote.webdriver
get_window_position
;文档有点含糊,但这正是函数所建议的:窗口而不是光标位置.
.location['x']
and.location['y']
on the element you pass intomove_to_element
. These values are not updated byActionChains
.- selenium.webdriver.remote.webdriver
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. - selenium.webdriver.remote.webdriver
get_window_position
; the documentation was a bit vague, but this does just what the function suggests: a window rather than a cursor position.
这篇关于在 Selenium 中显示当前光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!