Python selenium使用javascript设置textarea的值不起作用

2024-04-27 00:49:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我在开始时使用chrome驱动程序通过send_键方法发送文本。但根据this question,chrome驱动程序似乎不支持发送表情符号。因此,我使用脚本注入来根据链接发送表情符号

下面是代码,我使用send_键和javascript发送文本进行测试:

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)

它似乎是由这个图像工作。但是文本的数量是错误的。 enter image description here



单击网站上的文本区域后,只剩下send_key()发送的文本。 enter image description here

我知道我可以使用GeckoDriver根据this通过send_key()发送表情符号,但我想彻底理解,通过问题下方留下的评论似乎可以工作。 我测试的网站只能在你是卖家时才能看到,所以我无法提供测试的url

谢谢大家

[更新] 这是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>

Tags: key文本divtxtsendinputdataarea