的webdriver。按坐标单击Canvas元素

时间:2012-04-12 05:06:50

标签: ruby canvas selenium webdriver

我的页面上有canvas元素,我想点击它的某些部分。 我知道,我必须使用ActionBuilder这样做,所以我尝试了这段代码:

element = driver.find_element(:xpath, canvas_xpath)
action.move_to(element, 100, 100).click.perform

但是这段代码只点击画布元素的中心,不要以任何方式移动鼠标。

还有其他可能的方法将鼠标移动到某些坐标吗? (不要提及AutoIT脚本 - 我在Linux下开发)

3 个答案:

答案 0 :(得分:6)

我在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):(

答案 1 :(得分:1)

你试过action.move_to(element).move_by(100, 100).click.perform吗?

答案 2 :(得分:0)

相关问题