Reputation: 1028
I'm using selenium and Python and I am trying to execute a JS function that takes in a string like so:
browser.execute_script("foo('someString')")
This works when someString does not have any newlines. When there is a newline character the following happens:
\n => Parse Error
\\n => Newline is converted to a space
\r\n => Parse Error
\\r\\n => Both characters are converted to a space
Is there a way to do this?
Upvotes: 2
Views: 2790
Reputation: 1640
The easy solution is to convert \n
to unicode \u000a
.
similar example:
driver.execute_script("document.getElementById('website_name').value='hello1\u000ahello2'")
Upvotes: 1
Reputation: 369424
You can pass arguments to the javascript; those additional arguments are accessible using arguments
:
browser.execute_script("foo(arguments[0])", 'someString')
browser.execute_script("foo(arguments[0])", 'string with \n is also ok!\n')
Upvotes: 5