如何在watir中标记文本的某一部分?

时间:2017-08-29 16:59:49

标签: ruby watir

你好,有什么东西只能标记文本的某一部分吗? 我无法在任何地方找到合适的解决方案。

我试过了:double_clickflashselect_text对我没用。

这有效,但这标志着一切:browser.send_keys [:control, 'a']

我添加了示例图片,我想做什么。 谢谢您的回答。 The red rectangle shows the markings

1 个答案:

答案 0 :(得分:0)

您可以使用Element#select_text方法。请注意,在Watir 6.8之前,您需要手动包含扩展名(方法)。

以下是使用维基百科页面的工作示例:

require 'watir'
require 'watir/extensions/select_text' # only include this if using Watir 6.7 or prior

browser = Watir::Browser.new
browser.goto('https://en.wikipedia.org/wiki/XPath')
browser.body.select_text('XPath may be used')
sleep(5) # so that you can see the selection

请注意,这将突出显示第一场比赛。您可能希望限制搜索特定元素而不是整个body

以下是使用ckeditor.com的另一个例子:

require 'watir'
require 'watir/extensions/select_text' # only include this if using Watir 6.7 or prior

browser = Watir::Browser.new
browser.goto('ckeditor.com/')
frame = browser.iframe(class: 'cke_wysiwyg_frame')
frame.p.select_text('Bake the brownies')
browser.link(href: /Bold/).click
sleep(10)