36746

Selenium paste text into textarea

Question:

Using Selenium, I'd like to edit the contents of a textarea. Calling textarea.SendKeys("My text") works, but it types it in letter by letter which is obviously quite slow for longer strings.

One workaround I found (<a href="http://code.google.com/p/selenium/issues/detail?id=2876" rel="nofollow">http://code.google.com/p/selenium/issues/detail?id=2876</a>) suggests that I copy the string to a clipboard and paste it into the textarea. The Keys reference they give is ambiguous between OpenQA.Selenium.Keys and System.Windows.Forms.Keys, so I tried both of them. Here's my code to do that:

Clipboard.SetDataObject("My text"); textarea.SendKeys(OpenQA.Selenium.Keys.Control + "v");

This freezes the application. If I use System.Windows.Forms.Keys.Control instead, it types in controlv, which obviously is not what I want.

Does anyone have any idea as to what the problem might be?

Answer1:

I am able to use the same SendKeys when setting the clipboard to text:

Clipboard.SetText(trgt); myTextArea.SendKeys(OpenQA.Selenium.Keys.Control + "v");

so perhaps the SetDataObject is an issue

Answer2:

Hope this CopyPaste method helps:

using OpenQA.Selenium.Interactions; public void CopyPaste(string copy) { Clipboard.SetText(copy); new Actions(driver).SendKeys(OpenQA.Selenium.Keys.LeftShift + OpenQA.Selenium.Keys.Insert).Perform(); //because it switch to uppercase we do one more click new Actions(driver).SendKeys(OpenQA.Selenium.Keys.LeftShift).Perform(); } texarea.Click; // if driver refuse to click textarea you can force it with: //((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='MyTextareaId']"))); // not proven, but I think textarea.SendKeys(""); Will click inside the textarea //Call the method CopyPaste("Text Appear In the Textarea");

Recommend

  • Jquery - Copy div contents to clipboard
  • How do you copy the contents of a Cell into the Clipboard
  • How to add brackets using AutoHotKey
  • How to work with Clipboard in ASP.NET, Server side
  • Web API Basic Auth inside an MVC app with Identity Auth
  • SiteMesh: Changing the content-type of the response
  • Disadvantages to high make job values
  • const char **a = {“string1”,“string2”} and pointer arithametic
  • Can you render a page with multiple forms instead of one form for a jQuery, Rails 3 update?
  • Is it mandatory to have a doGet or doPost method?
  • Emacs lua-mode issue: (void-function interactively-called-p)
  • Want to understand iframe breakout code
  • Where these are stored?
  • Most efficient way to move table rows from one table to another
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Application level floating views with navigation in Android
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • What is the purpose of TaskExecutor in spring?
  • Array with custom indexes in Ionic2
  • Xcode 4 NSLog Macro link in Xcode 3
  • Functions in global context
  • Linq Objects Group By & Sum
  • Optimizing database types to compact database (SQLite)
  • Check if a string to interpolate provides expected placeholders
  • Javascript + PHP Encryption with pidCrypt
  • XCode can't find symbols for a specific iOS library/framework project
  • AT Commands to Send SMS not working in Windows 8.1
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • json Serialization in asp
  • Buffer size for converting unsigned long to string
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory