0

I use chrome driver to send text by send_key method in the beginning. But according to this question,chrome driver does not seem to support sending emoji. So I use a script injection to send emoji according to the link.

Here is the code, I use send_key and javascript to send text for testing:

reply_area = WebDriverWait(self.webdriver,1).until(expected_conditions.element_to_be_clickable(locator_))
reply_area.send_keys("Send_key")
JS_ADD_TEXT_TO_INPUT ="""
                      var element = arguments[0], txt = arguments[1];
                      element.value += txt;
                      element.dispatchEvent(new Event('change'));
                      """
txt = "\n" \
      "\n" \
      ""
self.webdriver.execute_script(JS_ADD_TEXT_TO_INPUT, reply_area, txt)

It seems work by this image. But the number of text is wrong. enter image description here



And after I click the text area on the website, only the text sent by send_key() is left. enter image description here

I know I can use GeckoDriver to send emoji by send_key() according to this, but I want to understand thoroughly, and it seems work by the comment left below the question. The website I tested can only be seen when you are a seller, so I can't provide the url for testing.

Thank you All.

[Updated] Here is the html.

<div data-v-4e626831="" class="reply-comment">
    <div data-v-4e626831="" class="shopee-input shopee-input__area">
        <textarea type="textarea" placeholder="Please enter your reply" resize="none" rows="2" minrows="2" maxlength="500" restrictiontype="input" class="shopee-input__inner shopee-input__inner--normal" style="resize: none; min-height: 71px;"></textarea> 
    </div>
    <div data-v-4e626831="" class="des">30/500</div> <!-- This is the number of word -->
</div>
  • 1
    Where's the html? – Pedro Lobito Mar 08 '20 at 13:56
  • @PedroLobito I provide the html. I don't know if this can provide enough information. – user7990701 Mar 09 '20 at 14:40
  • the effects you are seeing are probably red herrings and are the result of client-side code tracking interactions with the text area. If you instead submit the form after inserting the emojis, are they posted? – pcalkins Mar 09 '20 at 22:08
  • @pcalkins I actullay did the same thing as you said(submit the form after inserting), but the text still disappears automatically. Could you please explain more about 'red herrings' or provide other key words to this phenomenon? – user7990701 Mar 10 '20 at 13:56
  • try without using the .send_keys method and don't generate the change event. (So just the js setting the text...) https://www.mentalfloss.com/article/562812/where-did-phrase-red-herring-originate – pcalkins Mar 10 '20 at 16:57
  • it doesn't look like it was a red herring, btw... for whatever reason they don't seem to be using the textarea's value on submission... one thing that might work here is to populate the clipboard and then use Selenium to paste in the text. – pcalkins Mar 10 '20 at 18:16
  • @pcalkins I tried remove the change event, and it doesn't work. The screen just showed the alert "This feild can't be blank". I follow your another advice to use clipboard and it work. So the question remains why send_key is OK, but setting the value of textarea is not. – user7990701 Mar 11 '20 at 14:13
  • they may not be using a standard form which submits the value but rather tracking changes to the field client side. (basically they don't expect you to run javascript to update it...) – pcalkins Mar 11 '20 at 16:28

0 Answers0