繁体   English   中英

Ruby / Watir:如何单击禁用按钮(Watir::Exception::ObjectDisabledException)

[英]Ruby / Watir : how to click on a disabled button (Watir::Exception::ObjectDisabledException)

我想在 Ruby 中创建一个程序,创建一个 github 存储库。 一切都很好,但是当我想在填写存储库名称后单击“创建存储库”按钮时,什么也没发生,程序因超时错误而停止。

这是禁用按钮的 html 代码:

<button type="submit" class="btn btn-primary first-in-line" data-disable-with="Creating repository…" disabled="">
        Create repository
      </button>

以及启用按钮的 html 代码:

<button type="submit" class="btn btn-primary first-in-line" data-disable-with="Creating repository…">
        Create repository
      </button>

这是我的 ruby 程序

repo_name = gets.chomp
repo = browser.text_field(id: 'repository_name')
repo.set(repo_name)

browser.driver.manage.timeouts.implicit_wait = 3

create_button = browser.button(type: "submit")
create_button.wait_until(&:enabled?).click

我很确定我的 pb 来了,当我登陆页面时,按钮被禁用,即使我正在填写 repository_name 输入,我的 prog 也无法访问该按钮。

那么您对此有解决方案吗? 或者你知道是否还有其他铅?

编辑:

当这里是没有等待命令的代码时:

repo_name = gets.chomp
repo = browser.text_field(id: 'repository_name')
repo.set(repo_name)

create_button = browser.button(type: "submit").click

当我运行它时,我遇到了“Watir::Exception::ObjectDisabledException”错误

("element present, but timed out after 30 seconds, waiting for #
<Watir::Button: located: true; {:type=>"submit", :tag_name=>"button"}> to be enabled (Watir::Exception::ObjectDisabledException)"

我认为下面的代码可能对你有用

browser.text_field(id: 'repository_name').set("repo_name")
browser.send_keys(:tab)

正如贾斯汀在他的回答中提到的那样,单击创建存储库按钮,如下所示:

browser.button(type: "submit", visible: true).click 

问题是页面上有多个提交按钮。 您可以通过检索一组按钮来看到这一点:

# Button text:
browser.buttons(type: 'submit').map(&:text_content)
#=> ["Set status", "Sign out", "Create repository"]

# Disabled status:
browser.buttons(type: 'submit').map(&:disabled?)
#=> [true, false, false]

browser.button(type: "submit")返回页面上的第一个提交按钮,即禁用的“设置状态”按钮。

“创建存储库”按钮实际上是页面上的最后一个按钮。 需要更具体的按钮定位器。 一些选项:

# By text
browser.button(text: 'Create repository')

# By visibility (since the other 2 are hidden by default)
browser.button(type: "submit", visible: true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM