黄瓜+水豚+硒:选择文本 [英] Cucumber + Capybara + Selenium: selecting text

查看:144
本文介绍了黄瓜+水豚+硒:选择文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更改文本编辑器,我需要能够选择文本以使用JavaScript操作它。 如何选择包含Cucumber,Capybara和Selenium的文本?

I'm making changes to a text editor, and I need to be able to select text to manipulate it with JavaScript. How do I select text with Cucumber, Capybara and Selenium?

推荐答案

我发现了另一个stackoverflow问题讨论如何使用JavaScript选择文本。

I found another stackoverflow question that talks about how to select text using JavaScript.

您可以在JavaScript中设置和/或更改用户的文本选择吗?

我可以修改他们的脚本在Selenium IDE的getEval调用中工作(如果您以不同的方式使用Selenium,则可能需要修改它)。我也删除了与Firefox无关的代码:

I was able to modify their script to be able to work within a getEval call from Selenium IDE (if you're using Selenium in a different way, you may need to modify it). I also stripped out the code not pertinent to Firefox:

function selectElementContents(el,start,end) {
    var sel = window.getSelection();
    var range = window.document.createRange();
    range.setStart(el,start);
    range.setEnd(el,end);
    sel.removeAllRanges();
    sel.addRange(range);
}
selectElementContents(window.document.getElementById("container").firstChild,10,20)

这允许您在单个元素中选择文本范围。 container是包含文本的元素的ID,10和20是要开始和停止的字符的索引。

This allows you to select a range of text within a single element. "Container" is the ID of the element containing the text, and 10 and 20 are the indexes of characters to start and stop at.

只要将其缩小到一行,将它添加到Selenium getEval语句中,并且应该为您选择文本。

Just condense this down to one line, add it to a Selenium getEval statement, and it should select the text for you.

如果单个元素中的文本不够灵活,范围应该为您提供所需的额外细节。

If text within a single element isn't flexible enough for you, documentation on JavaScript ranges should provide you the extra detail you need.

这篇关于黄瓜+水豚+硒:选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆