当我搜索它存在的按钮时,但当我尝试点击它时,它找不到它并超时。我如何解决它? 我有更新的 chrome 驱动程序。我拥有最新级别的所有宝石。
irb(main):189:0> ie.button(value:"OK").html
=> "<input type=\"button\" onclick=\"javascript:makeList('1591993');\" value=\"OK\">"
irb(main):190:0> ie.button(value:"OK").exists?
=> true
irb(main):191:0> ie.button(value:"OK").click
2021-02-05 18:07:41 WARN Selenium [DEPRECATION] Selenium::WebDriver::Error::ElementNotVisibleError is deprecated. Use Selenium::WebDriver::Error::ElementNotInteractableError (ensure the driver supports W3C WebDriver specification) instead.
Traceback (most recent call last):
8: from C:/Ruby26-x64/bin/irb.cmd:31:in `<main>'
7: from C:/Ruby26-x64/bin/irb.cmd:31:in `load'
6: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
5: from (irb):191
4: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/watir-6.16.5/lib/watir/elements/element.rb:145:in `click'
3: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/watir-6.16.5/lib/watir/elements/element.rb:790:in `element_call'
2: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/watir-6.16.5/lib/watir/elements/element.rb:803:in `rescue in element_call'
1: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/watir-6.16.5/lib/watir/elements/element.rb:752:in `raise_present'
Watir::Exception::UnknownObjectException (element located, but timed out after 30 seconds, waiting for #<Watir::Button: located: true; {:value=>"OK", :tag_name=>"button"}> to be present)
答案 0 :(得分:0)
很可能有两个按钮的值为“OK”,当您编写 ie.button(value:"OK").click
时,它会点击第一个按钮,但它可能不可见。因此,如果有两个按钮,您看到的按钮可能是第二个按钮。所以首先你执行下面的代码
p ie.buttons(value:"OK").count
然后查看它是否打印 2 或 3(大于一个),如果打印,那么您实际上是在与第一个不可见的按钮进行交互。所以你要找出你想点击哪个按钮,如果count正好是2,那么你可以写如下代码。
ie.buttons(value:"OK").last.click #I used buttons here not button
这将发出对最后一个按钮的点击。如果按钮数超过两个,那么你可以通过按钮的索引来点击目标按钮,如下所示
ie.buttons(value:"OK")[3].last.click
答案 1 :(得分:0)
如果按钮存在但不可点击:
答案 2 :(得分:0)
fire_event 方法成功了 ie.button(value:"OK").fire_event "onclick"
答案 3 :(得分:0)
这在大多数情况下完成工作,将点击呈现一个
@b.buttons(text: "OK").select(&:present?).first.click
有时 watir 点击失败(有充分的理由)所以你用 javascript 来做,
@b.buttons(text: "OK").select(&:present?).first.fire_event :click
但您也可以尝试发送回车键
@b.buttons(text: "OK").select(&:present?).first.send_keys :enter
如果你看到预期的按钮收到点击,你可以验证它的工作