简体   繁体   English

如何使用Selenium和python将字符序列发送到contenteditable =“ true”元素

[英]How can i send character sequence to a contenteditable=“true” element by using selenium and python

lately, I try to use python3.6 and selenium to do a small task, I want to log in a chatting webpage and send the message automatically. 最近,我尝试使用python3.6和selenium做一个小任务,我想登录一个聊天网页并自动发送消息。

http://maimai.cn/?regfr=baidu_pc_title this is the webpage that I want to send the message to http://maimai.cn/?regfr=baidu_pc_title,这是我要将消息发送到的网页

In my limited experience with python, I only know that the input element can send string. 以我对python的有限经验,我只知道input元素可以发送字符串。 However, I came across this tough question. 但是,我遇到了这个难题。
I google a lot and I know that I need to use JavaScript in my python code to insert the string into a div element whose attribute contenteditable is true. 我在Google上搜索了很多,我知道我需要在python代码中使用JavaScript才能将字符串插入到contentcontentableable属性为true的div元素中。 when I run my code like this: 当我这样运行代码时:

my_desired_text="lzylzylzy" driver.execute_script("document.getElementsByClassName('inputPanel'[0].innerHTML="+ my_desired_text) my_desired_text =“ lzylzylzy” driver.execute_script(“ document.getElementsByClassName('inputPanel'[0] .innerHTML =” + my_desired_text)

it warns me like that: 它像这样警告我:

selenium.common.exceptions.WebDriverException: Message: unknown error: lzylzylzy is not defined selenium.common.exceptions.WebDriverException:消息:未知错误:lzylzylzy未定义

but I indeed define my string. 但是我确实定义了我的字符串。 I am searching for a long time on the net. 我在网上搜索了很长时间。 But no use, please help or try to give some ideas on how to achieve this. 但是没有用,请帮助或尝试提供一些有关如何实现此目标的想法。 Thanks in advance. 提前致谢。

在我的项目中,首先要放置一个iframe元素。我在iframe中找到了一个元素,但是我忘了切换到主框架。这个元素在主框架中,这就是为什么我找不到它的原因。如果遇到类似情况,则可以检查是否切换到主机并使用.sendkey()方法发送字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM