【问题标题】:Button exists but its not clickable按钮存在但不可点击
【发布时间】:2021-05-10 08:27:39
【问题描述】:

当我搜索它存在的按钮时,但当我尝试单击它时,它找不到它并超时。我如何解决它? 我有更新的 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)

【问题讨论】:

    标签: ruby selenium watir


    【解决方案1】:

    这在大多数情况下都可以完成工作,将点击呈现一个

    @b.buttons(text: "OK").select(&amp;:present?).first.click

    有时 watir 点击失败(有充分的理由),所以你用 javascript 来做,

    @b.buttons(text: "OK").select(&amp;:present?).first.fire_event :click

    但你也可以尝试发送回车键

    @b.buttons(text: "OK").select(&amp;:present?).first.send_keys :enter

    如果您看到预期的按钮收到点击,您可以验证它的工作

    【讨论】:

      【解决方案2】:

      fire_event 方法成功了 ie.button(value:"OK").fire_event "onclick"

      【讨论】:

      • Fire_Event 正在使用 javascript,这不是 selenium 点击。您可能点击了不可见的错误元素。
      【解决方案3】:

      如果按钮存在但不可点击:

      1. 不可见:先手动打开页面检查,调整 CSS
      2. 它被什么东西挡住了。这可能是一个透明块,上面有更高的 z-index,或者按钮被部分阻止。尝试将按钮移动到另一个开阔的空间,然后再次运行脚本。

      【讨论】:

      • 这些都不应该是原因。按钮存在时可能不可见。
      【解决方案4】:

      很有可能有两个按钮的值为“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
      

      【讨论】:

        猜你喜欢
        • 2020-11-24
        • 2015-02-25
        • 1970-01-01
        • 2015-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-31
        • 2015-09-26
        相关资源
        最近更新 更多