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?
相关问题
- Selecting an item from a combo box selenium driver
- Selenium in Java is not finding element when using
- How to send text to the search field through Selen
- What is the difference in “find_element_by_xpath”
- Cloudflare and Chromedriver - cloudflare distingui
Tying the approach suggested by Josh into a Capybara helper function (and adding optional position arguments for partial selections within the start/end elements):
Then, in your test:
I found another stackoverflow question that talks about how to select text using JavaScript.
Can you set and/or change the user’s text selection in JavaScript?
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:
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.
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.