【问题标题】:Submit text field using watir使用 watir 提交文本字段
【发布时间】:2018-08-01 00:59:24
【问题描述】:

我正在尝试使用 watir 以编程方式将文本提交到文本字段中。我正在使用 Stack Overflow 作为脚本的测试站点。

我能够引用搜索字段 (name='q'),但提交按钮没有 idname 属性。那么如何以编程方式引用提交按钮并单击它呢?

这是我目前的代码:

require 'watir'
require 'pry'

browser = Watir::Browser.new :chrome, headless: true

browser.goto 'https://www.stackoverflow.com'
browser.text_field(:name, "q").set('user:963076') #the search bar name='q'
browser.button(#how do I reference the submit button?, '').click
browser.screenshot.save 'screenafter.png' #the screenshot is so that I know what happened after clicking

browser.close
browser.quit

直接回答这个问题很好,但解释一下如何自己找到解决此类问题的方法会更好,非常感谢。

【问题讨论】:

  • 我刚刚更新了 Watir 网站,提供了关于如何使用 Watir 定位器的指南:watir.com/guides/locating 另外,我们建议现在使用哈希作为选择器,而不是使用有序的一对参数(如何,什么) ,因为可以根据需要组合多个定位器。
  • @titusfortner 嗨,这是一本优秀的读物,这个页面http://xpathify.herokuapp.com/ 太棒了!感谢您在此提及。

标签: ruby watir


【解决方案1】:

你必须点击这个按钮

<button type="submit" aria-label="Search..." class="s-btn s-btn__primary btn-topbar-primary js-search-submit"><svg aria-hidden="true" class="svg-icon mx0 iconSearch" width="18" height="18" viewBox="0 0 18 18"><path d="M12.86 11.32L18 16.5 16.5 18l-5.18-5.14v-.35a7 7 0 1 1 1.19-1.19h.35zM7 12A5 5 0 1 0 7 2a5 5 0 0 0 0 10z"></path></svg></button>

按钮的type属性值为submit,所以我们可以用它来定位那个按钮,这里是代码

browser.goto 'https://www.stackoverflow.com'
browser.text_field(name: "q").set('user:963076')
browser.button(type: 'submit').click

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多