0

I'm trying to insert a HTML code (around 3000 letters) into a Textarea. There is .sendkeys, however that's very slow. So I searched and found a way to use javascript with selenium and it worked fine till I reached the point where I had to add HTML code. My current code:

public void AttributeSet(string id, string value) {
    IJavaScriptExecutor js = (IJavaScriptExecutor)browser;      
    js.ExecuteScript("document.getElementById(\"" + id + "\").value = ('" + value + "');");
}

I realized that it works just fine with a single line of string, however it has a hard time with multiline strings and strings which contain quotes "" in them.

Since HTML code has a bunch of quotes it keeps preventing me to do so. What I tried so far is changing my string (newlines to \n, " to \") and a bunch of other things which I forgot.

Is there an easy way to do this? If yes please help me out I'd appreciate it very much!

AP.
  • 8,082
  • 2
  • 24
  • 33
Samo_
  • 21
  • 1
  • 5
  • What browser is this for? if it IE there is a known issue with the 64-bit version of the driver and the .sendKeys() typing slow. http://stackoverflow.com/questions/27985300/selenium-webdriver-typing-very-slow-in-text-field-on-ie-browser – Buster Mar 21 '17 at 21:15
  • @JoshuaBurns Hi Joshua, it would still be slow since it's a large amount of characters thats why I prefer to just change the value of an area instantly. – Samo_ Mar 21 '17 at 22:21
  • Seems like you could just escape all the problem characters before inserting them. What is your specific problem? – JeffC Mar 21 '17 at 22:49
  • @JeffC All I want to do is insert a large amount of htmlcode into a textarea with selenium using javascript. My issue is that it gives an error because of multilines and I assume because of the quotations, \'s, and more. – Samo_ Mar 21 '17 at 23:21
  • We can't really help until you narrow down the problem. You haven't provided a sample string or error messages or much else. Break the string down into smaller parts and insert each one until you run into an issue. Find the problem character and replace it. Move on to the next section. – JeffC Mar 22 '17 at 01:24
  • @JeffC I've already stated what the issues were in the post. Quotes, newlines and \'s. If I replace the quotes the html code will no longer be valid. Replacing a certain character wont help me progress. – Samo_ Mar 22 '17 at 11:10
  • Sure it will... you just escape each of the problem characters and it works. People do it all the time. If you do some googling, you'll see all kinds of ways to do this successfully. – JeffC Mar 22 '17 at 13:27

2 Answers2

0

What browsers are you using? If you're using any moderately modern browser it should support es6 multiline strings.

In order to use multiline string change single quote character ' to backtick `.

js.ExecuteScript("document.getElementById(\"" + id + "\").value = (`" + value + "`);");
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
  • Hi Randall I'm using PhantomJS (Headless browser) I'll try what you said in a moment, thanks in advance :) – Samo_ Mar 21 '17 at 22:20
  • Randall, unfortunately it didn't work. Now the string which aren't multiline aren't working anymore either. {"errorMessage":Invalid character: '`'. – Samo_ Mar 21 '17 at 22:29
0

I figured it out, you add a reference to System.Web and than proceed to encode the string by doing

HttpUtility.JavaScriptStringEncode(value);

Thanks to everyone who tried to help.

Samo_
  • 21
  • 1
  • 5