0
votes

I'm creating a test for the forum and I would like to test the forum BB code. I have a BB code like this,

[B][FONT=Trebuchet MS][SIZE=7]Meteor[/SIZE][/FONT][/B]
[COLOR=#000000][FONT=Verdana][I][B][SIZE=4]n: [/B][/I][/FONT]
[S][B][FONT=Verdana]ki[/SIZE][/FONT][/B][/S][/COLOR]
[URL='https:///coral-lps-6/']
[FONT=Impact][SIZE=7][COLOR=#F41600][U]CLICK HERE TO BUY![/U][/COLOR][/SIZE][/FONT][/URL]

I'm trying to populate this code in to the text area(wrapped in p tag) using the following way,

messageBox = self.browser.find_elements_by_css_selector('.message-editorWrapper p')[0]
code = r"{}".format(bb_code)
self.browser.execute_script("arguments[0].innerHTML = '"+code+"'", messageBox)

I'm getting this error

Traceback (most recent call last): File "login.py", line 51, in bot.postRequest() File "login.py", line 31, in postRequest self.browser.execute_script("arguments[0].innerHTML = '"+code+"'", messageBox) File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 636, in execute_script 'args': converted_args})['value'] File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.evaluate threw exception: SyntaxError: Invalid or unexpected token (Session info: chrome=70.0.3538.77) (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Linux 4.15.0-66-generic x86_64)

But when I use simple text that is working fine. So how I can add the BB CODE in the forum to test that it is working or not using selenimum.

1
you put code in ' ' in innerHTML='...' but inside BB code you have also ' in [URL='https:///coral-lps-6/'] - maybe it makes problem. It can create incorrect JavaScript code. Maybe you should use " " in innerHTML="..." or in BB code. - furas
this is useless r"{}".format(bb_code) because it add nothing to bb_code - it will not add " " to code. - furas

1 Answers

1
votes

I will guess.

You put code in ' ' in innerHTML='...' but inside BB code you have also ' in [URL='https:///coral-lps-6/'] and probably it can makes problem. It can create incorrect JavaScript code like

'code'https:///coral-lps-6/'code'

which can be treated as three elements

'code' https:///coral-lps-6/ 'code'

Maybe you should use " " in innerHTML="..." or in BB code.

js = 'arguments[0].innerHTML = "{}"'.format(code)

BTW: r"{}".format(bb_code) is useless. It change nothing in bb_code. And for sure it doesn't add " " to bb_code. You would need external ' ' to add " " - r'"{}"'.format(bb_code)


EDIT: because BB code is in many lines so it may need \ at the end of lines to inform JavaScript that it is not end of string.

bb_code = '''[B][FONT=Trebuchet MS][SIZE=7]Meteor[/SIZE][/FONT][/B]\
[COLOR=#000000][FONT=Verdana][I][B][SIZE=4]n: [/B][/I][/FONT]\
[S][B][FONT=Verdana]ki[/SIZE][/FONT][/B][/S][/COLOR]\
[URL='https:///coral-lps-6/']\
[FONT=Impact][SIZE=7][COLOR=#F41600][U]CLICK HERE TO BUY![/U][/COLOR][/SIZE][/FONT][/URL]'''

js = 'arguments[0].innerHTML = "{}"'.format(bb_code)

print(js)

self.browser.execute_script(js, messageBox)

EDIT: This code works for me. It puts BB code in text area for answer on current page.

import selenium.webdriver

driver = selenium.webdriver.Firefox()

driver.get('https://stackguides.com/questions/58748338/fill-textarea-with-the-bb-code-in-selenium-python/58748608')

messageBox = driver.find_element_by_id('wmd-input')

bb_code = '''[B][FONT=Trebuchet MS][SIZE=7]Meteor[/SIZE][/FONT][/B]\
[COLOR=#000000][FONT=Verdana][I][B][SIZE=4]n: [/B][/I][/FONT]\
[S][B][FONT=Verdana]ki[/SIZE][/FONT][/B][/S][/COLOR]\
[URL='https:///coral-lps-6/']\
[FONT=Impact][SIZE=7][COLOR=#F41600][U]CLICK HERE TO BUY![/U][/COLOR][/SIZE][/FONT][/URL]'''

js = 'arguments[0].innerHTML = "{}"'.format(bb_code)

#print(js)

driver.execute_script(js, messageBox)