Gauthier Buttez
Gauthier Buttez

Reputation: 1193

How to write some text in a span and submit it by pressing "ENTER" with Selenium Python 3.7?

Environment:

Python 3.7

Selenium 3.141

Problem :

I have a page with some posts. I would like selenium to add a comment for some post.

The form to add a comment is not an "INPUT" field, it is a SPAN without button "Submit". So the idea, is to write some text and press enter. You can see this kind of "commenting" actions in many social networks : no button "submit" and a span which collect the text.

I manage to write the text with a javascript inside my selenium :

insertComment = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
        (By.XPATH, ("(//div[contains(@class,\"_1mf _1mj\")]//span)[1]"))))

driver.execute_script("arguments[0].innerText = 'test\n'", insertComment)

As you can see above, I tried to press ENTER with a \n, but it failed:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1483, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:/Dropbox/cff/Python/projectV3/tmp.py", line 181, in <module>
    p_browser, p_taskuser_id, website_Username, 10, label_log, lock)
  File "E:/Dropbox/cff/Python/projectV3/tmp.py", line 166, in Comment_website_Posts_By_Keyword_1905
    driver.execute_script("arguments[0].innerText = 'test\n'", insertComment)
  File "C:\Users\gauth\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 636, in execute_script
    'args': converted_args})['value']
  File "C:\Users\gauth\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\gauth\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: SyntaxError: '' string literal contains an unescaped line break

Do you know how can I press enter using this javascript method as the "sendkeys(Keys.ENTER)" or "sendkeys(Keys.RETURN)" doesn't work:

Element <span> is not reachable by keyboard

Here is the html code of the form:

<form
    class="o6r2urh6 l9j0dhe7 b3i9ofy5 e72ty7fz qlfml3jp inkptoze qmr60zad rt8b4zig n8ej3o3l agehan2d sk4xxmp2 j83agx80 bkfpd7mw lhclo0ds"
    role="presentation">
    <div class="oo9gr5id o6r2urh6 mg4g778l buofh1pr g5gj957u jq4qci2q ni8dbmo4 stjgntxs cxgpxx05 d1544ag0 sj5x9vvc tw6a2znq"
        data-visualcompletion="ignore">
        <div class="_5rp7">
            <div class="_5rpb">
                <div aria-label="Write a comment" class="notranslate _5rpu" contenteditable="true" role="textbox"
                    spellcheck="false"
                    style="outline: none; user-select: text; white-space: pre-wrap; overflow-wrap: break-word;">
                    <div data-contents="true">
                        <div class="" data-block="true" data-editor="c5m3a" data-offset-key="a0f66-0-0">
                            <div data-offset-key="a0f66-0-0" class="_1mf _1mj"><span data-offset-key="a0f66-0-0"><span
                                        data-text="true">sdsdds sd</span></span></div>
                        </div>
                    </div>
                </div>
                <grammarly-extension data-grammarly-shadow-root="true"
                    style="position: absolute; top: 0px; left: -0.8px; pointer-events: none; z-index: auto;"
                    class="cGcvT"></grammarly-extension>
                <grammarly-extension data-grammarly-shadow-root="true"
                    style="mix-blend-mode: darken; position: absolute; top: 0px; left: -0.8px; pointer-events: none; z-index: auto;"
                    class="cGcvT"></grammarly-extension>
            </div>
        </div>
    </div>
    <ul class="fop5sh7t cgat1ltu tv7at329 j83agx80 c4hnarmi bp9cbjyn">
        <li class="ggphbty4 fv0vnmcu q9uorilb"><span
                class="tojvnm2t a6sixzi8 abs2jz4q a8s20v7p t1p8iaqh k5wvi7nf q3lfd5jv pk4s997a bipmatt0 cebpdrjk qowsmv63 owwhemhu dp1hu0rb dhp61c6y iyyx5f41">
                <div aria-label="Insert an emoji"
                    class="oajrlxb2 gs1a9yip g5ia77u1 mtkw9kbi tlpljxtp qensuy8j ppp5ayq2 goun2846 ccm00jje s44p3ltw mk2mc5f4 rt8b4zig n8ej3o3l agehan2d sk4xxmp2 rq0escxv nhd2j8a9 pq6dq46d mg4g778l btwxx1t3 pfnyh3mw p7hjln8o kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x tgvbjcpo hpfvmrgz jb3vyjys rz4wbd8a qt6c0cv9 a8nywdso l9j0dhe7 i1ao9s8h esuyzwwr f1sip0of du4w35lb lzcic4wl abiwlrkh p8dawk7l dwo3fsh8 pzggbiyp pkj7ub1o bqnlxs5p kkg9azqs c24pa1uk ln9iyx3p fe6kdd0r ar1oviwq l10q8mi9 sq40qgkc s8quxz6p pdjglbur"
                    role="button" tabindex="0"><i data-visualcompletion="css-img" class="hu5pjgll m6k467ps"
                        style="background-position:-18px -252px;background-size:74px 376px;width:16px;height:16px;background-repeat:no-repeat;display:inline-block"></i>
                    <div class="s45kfl79 emlxlaya bkmhp75w spb7xbtv i09qtzwb n7fi1qx3 b5wmifdl hzruof5a pmk7jnqg j9ispegn kr520xx4 c5ndavph art1omkt ot9fgl3s"
                        data-visualcompletion="ignore" style="bottom:-8px;left:-8px;right:-8px;top:-8px"></div>
                </div>
            </span></li>
        <li class="ggphbty4 fv0vnmcu q9uorilb"><span
                class="tojvnm2t a6sixzi8 abs2jz4q a8s20v7p t1p8iaqh k5wvi7nf q3lfd5jv pk4s997a bipmatt0 cebpdrjk qowsmv63 owwhemhu dp1hu0rb dhp61c6y iyyx5f41">
                <div aria-label="Attach a photo or video"
                    class="oajrlxb2 gs1a9yip g5ia77u1 mtkw9kbi tlpljxtp qensuy8j ppp5ayq2 goun2846 ccm00jje s44p3ltw mk2mc5f4 rt8b4zig n8ej3o3l agehan2d sk4xxmp2 rq0escxv nhd2j8a9 pq6dq46d mg4g778l btwxx1t3 pfnyh3mw p7hjln8o kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x tgvbjcpo hpfvmrgz jb3vyjys rz4wbd8a qt6c0cv9 a8nywdso l9j0dhe7 i1ao9s8h esuyzwwr f1sip0of du4w35lb lzcic4wl abiwlrkh p8dawk7l dwo3fsh8 pzggbiyp pkj7ub1o bqnlxs5p kkg9azqs c24pa1uk ln9iyx3p fe6kdd0r ar1oviwq l10q8mi9 sq40qgkc s8quxz6p pdjglbur"
                    role="button" tabindex="0"><i data-visualcompletion="css-img" class="hu5pjgll m6k467ps"
                        style="background-position:-18px -234px;background-size:74px 376px;width:16px;height:16px;background-repeat:no-repeat;display:inline-block"></i>
                    <div class="s45kfl79 emlxlaya bkmhp75w spb7xbtv i09qtzwb n7fi1qx3 b5wmifdl hzruof5a pmk7jnqg j9ispegn kr520xx4 c5ndavph art1omkt ot9fgl3s"
                        data-visualcompletion="ignore" style="bottom:-8px;left:-8px;right:-8px;top:-8px"></div>
                </div>
            </span><input type="file"
                accept="video/*,  video/x-m4v, video/webm, video/x-ms-wmv, video/x-msvideo, video/3gpp, video/flv, video/x-flv, video/mp4, video/quicktime, video/mpeg, video/ogv, .ts, .mkv, image/*, image/heic, image/heif"
                class="mkhogb32"></li>
        <li class="ggphbty4 fv0vnmcu q9uorilb"><span
                class="tojvnm2t a6sixzi8 abs2jz4q a8s20v7p t1p8iaqh k5wvi7nf q3lfd5jv pk4s997a bipmatt0 cebpdrjk qowsmv63 owwhemhu dp1hu0rb dhp61c6y iyyx5f41">
                <div aria-label="Comment with a GIF"
                    class="oajrlxb2 gs1a9yip g5ia77u1 mtkw9kbi tlpljxtp qensuy8j ppp5ayq2 goun2846 ccm00jje s44p3ltw mk2mc5f4 rt8b4zig n8ej3o3l agehan2d sk4xxmp2 rq0escxv nhd2j8a9 pq6dq46d mg4g778l btwxx1t3 pfnyh3mw p7hjln8o kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x tgvbjcpo hpfvmrgz jb3vyjys rz4wbd8a qt6c0cv9 a8nywdso l9j0dhe7 i1ao9s8h esuyzwwr f1sip0of du4w35lb lzcic4wl abiwlrkh p8dawk7l dwo3fsh8 pzggbiyp pkj7ub1o bqnlxs5p kkg9azqs c24pa1uk ln9iyx3p fe6kdd0r ar1oviwq l10q8mi9 sq40qgkc s8quxz6p pdjglbur"
                    role="button" tabindex="0"><i data-visualcompletion="css-img" class="hu5pjgll m6k467ps"
                        style="background-position:-36px -252px;background-size:74px 376px;width:16px;height:16px;background-repeat:no-repeat;display:inline-block"></i>
                    <div class="s45kfl79 emlxlaya bkmhp75w spb7xbtv i09qtzwb n7fi1qx3 b5wmifdl hzruof5a pmk7jnqg j9ispegn kr520xx4 c5ndavph art1omkt ot9fgl3s"
                        data-visualcompletion="ignore" style="bottom:-8px;left:-8px;right:-8px;top:-8px"></div>
                </div>
            </span></li>
        <li class="ggphbty4 fv0vnmcu q9uorilb"><span
                class="tojvnm2t a6sixzi8 abs2jz4q a8s20v7p t1p8iaqh k5wvi7nf q3lfd5jv pk4s997a bipmatt0 cebpdrjk qowsmv63 owwhemhu dp1hu0rb dhp61c6y iyyx5f41">
                <div aria-label="Comment with a Sticker"
                    class="oajrlxb2 gs1a9yip g5ia77u1 mtkw9kbi tlpljxtp qensuy8j ppp5ayq2 goun2846 ccm00jje s44p3ltw mk2mc5f4 rt8b4zig n8ej3o3l agehan2d sk4xxmp2 rq0escxv nhd2j8a9 pq6dq46d mg4g778l btwxx1t3 pfnyh3mw p7hjln8o kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x tgvbjcpo hpfvmrgz jb3vyjys rz4wbd8a qt6c0cv9 a8nywdso l9j0dhe7 i1ao9s8h esuyzwwr f1sip0of du4w35lb lzcic4wl abiwlrkh p8dawk7l dwo3fsh8 pzggbiyp pkj7ub1o bqnlxs5p kkg9azqs c24pa1uk ln9iyx3p fe6kdd0r ar1oviwq l10q8mi9 sq40qgkc s8quxz6p pdjglbur"
                    role="button" tabindex="0"><i data-visualcompletion="css-img" class="hu5pjgll m6k467ps"
                        style="background-position:-36px -270px;background-size:74px 376px;width:16px;height:16px;background-repeat:no-repeat;display:inline-block"></i>
                    <div class="s45kfl79 emlxlaya bkmhp75w spb7xbtv i09qtzwb n7fi1qx3 b5wmifdl hzruof5a pmk7jnqg j9ispegn kr520xx4 c5ndavph art1omkt ot9fgl3s"
                        data-visualcompletion="ignore" style="bottom:-8px;left:-8px;right:-8px;top:-8px"></div>
                </div>
            </span></li>
    </ul>
</form>

Upvotes: 0

Views: 532

Answers (1)

TimmyMcTimmy
TimmyMcTimmy

Reputation: 1

could you use pyautogui to press enter? sorry if I can't help

Upvotes: 0

Related Questions