I'm trying to have Python run some Javascript, and I am running into a few issues. Namely that when I am trying to inject some text into a textarea of a page, it doesn't seem to work if and only if there is a newline (\n) as part of the variable inserted for the Javascript.
Here's my line:
br.runjs("document.getElementById('edit-body').value = '%s'" % (brag))
if the variable "brag" has any sort of newline in it at all, the insertion does not work. Any idea how to go around this?
asked Jul 6, 2011 at 22:50
Steven Matthews
11.7k50 gold badges140 silver badges260 bronze badges
1 Answer 1
Encode brag as JSON and then use that.
answered Jul 6, 2011 at 22:52
Ignacio Vazquez-Abrams
804k160 gold badges1.4k silver badges1.4k bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Steven Matthews
Do you have an example of how to do that? My Javascript is far less good than my Python, I can only do basic things (in order to interact with web pages). Working on picking up more, but still not there.
Ignacio Vazquez-Abrams
>>> print "var foo = %s" % (json.dumps('bar'),) var foo = "bar"default