我需要使用 selenium-chromedriver 在电报机器人中发送复制的文本
I need send copied text in telegram bot using selenium-chromedriver
element = browser.find_element_by_xpath("//div[@class='span12']")
# send the copied text back
context.bot.send_message(chat_id=update.message.chat_id, text=element)
#出现错误
element = browser.find_element_by_xpath("//div[@class='span12']")
2021-10-15 05:34:51,229 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception
........................
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type WebElement is not JSON serializable
JSON
只能发送原始类型的数据,但您有对象 WebElement
.
您应该手动将其转换为字符串
send_message(..., text=str(element) )
但您可能只是忘记了 .text
从该对象中获取文本。
send_message(..., text=element.text )
我找到了解决方案:
将以下行添加到您的代码中:
content = browser.find_element_by_id('report').text
完整示例:
content = browser.find_element_by_id('report').text
# send the copid text back
context.bot.send_message(chat_id=update.message.chat_id, text=content)
element = browser.find_element_by_xpath("//div[@class='span12']")
# send the copied text back
context.bot.send_message(chat_id=update.message.chat_id, text=element)
#出现错误
element = browser.find_element_by_xpath("//div[@class='span12']")
2021-10-15 05:34:51,229 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception
........................
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type WebElement is not JSON serializable
JSON
只能发送原始类型的数据,但您有对象 WebElement
.
您应该手动将其转换为字符串
send_message(..., text=str(element) )
但您可能只是忘记了 .text
从该对象中获取文本。
send_message(..., text=element.text )
我找到了解决方案:
将以下行添加到您的代码中:
content = browser.find_element_by_id('report').text
完整示例:
content = browser.find_element_by_id('report').text
# send the copid text back
context.bot.send_message(chat_id=update.message.chat_id, text=content)