selenium execute_script 带换行符
selenium execute_script with newlines
我正在使用 selenium 和 Python,我正在尝试执行一个接受字符串的 JS 函数,如下所示:
browser.execute_script("foo('someString')")
这在 someString 没有任何换行符时有效。
当有换行符时,会发生以下情况:
\n => Parse Error
\n => Newline is converted to a space
\r\n => Parse Error
\r\n => Both characters are converted to a space
有办法吗?
您可以将参数传递给 javascript;可以使用 arguments
:
访问这些附加参数
browser.execute_script("foo(arguments[0])", 'someString')
browser.execute_script("foo(arguments[0])", 'string with \n is also ok!\n')
简单的解决方案是将 \n
转换为 unicode \u000a
。
类似例子:
driver.execute_script("document.getElementById('website_name').value='hello1\u000ahello2'")
我正在使用 selenium 和 Python,我正在尝试执行一个接受字符串的 JS 函数,如下所示:
browser.execute_script("foo('someString')")
这在 someString 没有任何换行符时有效。 当有换行符时,会发生以下情况:
\n => Parse Error
\n => Newline is converted to a space
\r\n => Parse Error
\r\n => Both characters are converted to a space
有办法吗?
您可以将参数传递给 javascript;可以使用 arguments
:
browser.execute_script("foo(arguments[0])", 'someString')
browser.execute_script("foo(arguments[0])", 'string with \n is also ok!\n')
简单的解决方案是将 \n
转换为 unicode \u000a
。
类似例子:
driver.execute_script("document.getElementById('website_name').value='hello1\u000ahello2'")