我的页面上有画布元素,我想点击它的某些部分。我知道,我必须使用 ActionBuilder 来执行此操作,所以我尝试了以下代码:
element = driver.find_element(:xpath, canvas_xpath)
action.move_to(element, 100, 100).click.perform
但是此代码仅单击画布元素的中心,并且不以任何方式移动鼠标。
有没有其他可能的方法将鼠标移动到某个坐标?(不提 AutoIT 脚本——我在 Linux 下开发)
我在 IE 中遇到了同样的问题。ShockwaveNN 的代码适用于 Firefox 和 Chrome。我认为问题在于元素中间的“点击”点击。以下是 action_builder.rb 中的文档:
#
# Clicks in the middle of the given element. Equivalent to:
#
# driver.action.move_to(element).click
#
# When no element is passed, the current mouse position will be clicked.
#
# @example Clicking on an element
#
# el = driver.find_element(:id, "some_id")
# driver.action.click(el).perform
#
# @example Clicking at the current mouse position
#
# driver.action.click.perform
#
# @param [Selenium::WebDriver::Element] element An optional element to click.
# @return [ActionBuilder] A self reference.
#
根据这个和我的结论,应该只是在两行中执行这些操作,例如:
element = driver.find_element(:xpath, canvas_xpath)
driver.action.move_to(element, 100, 100).perform
driver.action.click.perform
或者
element = driver.find_element(:xpath, canvas_xpath)
driver.action.move_to(element).perform
driver.action.move_by(100, 100).click.perform
可悲的是,这些都不起作用(在 IE 中对我来说):(
你试过了action.move_to(element).move_by(100, 100).click.perform吗?
点击发生在目标元素的中心(参见评论 3 和 5 http://code.google.com/p/selenium/issues/detail?id=3578)
命令:http : //selenium.googlecode.com/svn/trunk/rb/lib/selenium/webdriver/remote/commands.rb ActionBuilder:http ://selenium.googlecode.com/svn/trunk/rb/lib/selenium /webdriver/common/action_builder.rb