ruby+watir 处理弹出框问题中 click_no_wait无法使用时的解决方法

  watir-1.65中 click_no_wait 会出现一种情况无法对BUTTON进行点击。

  以下提供一种方法可以代替click_no_wait

#named as MouseClick.rb

  require 'watir'
require 'Win32API'
module Watir
  class Element
    def top_edge
      assert_exists
      assert_enabled
      ole_object.getBoundingClientRect.top.to_i
    end

    def top_edge_absolute
      top_edge + page_container.document.parentWindow.screenTop.to_i
    end

    def left_edge
      assert_exists
      assert_enabled
      ole_object.getBoundingClientRect.left.to_i
    end

    def left_edge_absolute
      left_edge + page_container.document.parentWindow.screenLeft.to_i
    end

    #功能说明:
    #- 模拟点击鼠标右键
    #
    #参数说明:
    #- 无
    #
    #调用示例:
    #-require 'MouseClick.rb'
    #- ie.button(:name,"name").right_click
    #
    #返回值说明:
    #-  无
    def right_click
      x = left_edge_absolute
      y = top_edge_absolute
      #puts "x: #{x}, y: #{y}"
      WindowsInput.move_mouse(x, y)
      WindowsInput.right_click
    end

    #功能说明:
    #- 模拟点击鼠标左键
    #
    #参数说明:
    #- 无
    #
    #调用示例:
    #-require 'MouseClick.rb'
    #-ie.button(:name,"name").left_click
    #
    #返回值说明:
    #-  无
    def left_click
      x = left_edge_absolute
      y = top_edge_absolute
      #puts "x: #{x}, y: #{y}"
      # need some extra push to get the cursor in the right area
      WindowsInput.move_mouse(x + 2, y + 2)
      WindowsInput.left_click
    end
  end
end

module WindowsInput
  # Windows API functions
  SetCursorPos = Win32API.new('user32','SetCursorPos', 'II', 'I')
  SendInput = Win32API.new('user32','SendInput', 'IPI', 'I')
  # Windows API constants
  INPUT_MOUSE = 0
  MOUSEEVENTF_LEFTDOWN = 0x0002
  MOUSEEVENTF_LEFTUP = 0x0004
  MOUSEEVENTF_RIGHTDOWN = 0x0008
  MOUSEEVENTF_RIGHTUP = 0x0010

  module_function

  def send_input(inputs)
    n = inputs.size
    ptr = inputs.collect {|i| i.to_s}.join # flatten arrays into single string
    SendInput.call(n, ptr, inputs[0].size)
  end

  def create_mouse_input(mouse_flag)
    mi = Array.new(7, 0)
    mi[0] = INPUT_MOUSE
    mi[4] = mouse_flag
    mi.pack('LLLLLLL') # Pack array into a binary sequence usable to SendInput
  end

  def move_mouse(x, y)
    SetCursorPos.call(x, y)
  end

  def right_click
    rightdown = create_mouse_input(MOUSEEVENTF_RIGHTDOWN)
    rightup = create_mouse_input(MOUSEEVENTF_RIGHTUP)
    send_input( [rightdown, rightup] )
  end

  def left_click
    leftdown = create_mouse_input(MOUSEEVENTF_LEFTDOWN)
    leftup = create_mouse_input(MOUSEEVENTF_LEFTUP)
    send_input( [leftdown, leftup] )
  end
end

写脚本时候只需作以下调用

require "MouseClick"

ie=Watir::IE.new

ie.goto(url)

ie.bring_to_front

ie.button(:id,"Button1").left_click

sleep 1

ie.button(:id,"Button1").transactdialog

注:1 IE窗口必须100%大小 且放置在最前ie.bring_to_front

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值