Cucumber + Capybara + Selenium:选择文本 [英] Cucumber + Capybara + Selenium: selecting text

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

问题描述

我正在对文本编辑器进行更改,我需要能够选择文本以使用 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?

推荐答案

我发现另一个关于如何使用 JavaScript 选择文本的 stackoverflow 问题.

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.

如果单个元素中的文本对您来说不够灵活,那么有关 JavaScript 范围的文档应该为您提供所需的额外详细信息.

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

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

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