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

How to mark a certain part of the text in watir?

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

我试过了:double_clickflashselect_text 对我不起作用。

这行得通,但这标记了所有内容:browser.send_keys [:control, 'a']

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

您可以使用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)