首页 > 解决方案 > 我需要使用 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

标签: pythonselenium-webdriverxpathselenium-chromedrivertelegram-bot

解决方案


JSON只能发送原始类型的数据,但你有 object WebElement

您应该手动将其转换为字符串

send_message(..., text=str(element) )

但可能您只是忘记.text从该对象获取文本。

send_message(..., text=element.text )

推荐阅读