TypeError: object of type 'ID3TimeStamp' has no len()

TypeError: object of type 'ID3TimeStamp' has no len()

我编写了这段代码,从 mp3 中获取年份,如果我打印它,它可以工作,但是当我在我的网页中写入文本框时,它给出了一个错误(下面的回溯),但没有总是,有时错误不显示,所以我怀疑这是来自 mp3 的标记方式:

nfo_year = ''
audio_filename = 'myfile.mp3'

f = mutagen.File(audio_filename)

audio = ID3(audio_filename)  # path: path to file


# Year
try:
    nfo_year = audio['TDRC'].text[0]
    print(nfo_year)
except:
    pass

time.sleep(2)
logger_internet.info('Writing Year...')
AB_author = driver.find_element_by_name('year')
AB_author.send_keys(nfo_year)





Traceback (most recent call last):
  File "D:\AB\redacted.py", line 1252, in <module>
    AB_author.send_keys(nfo_year)
  File "D:\AB\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 478, in send_keys
    {'text': "".join(keys_to_typing(value)),
  File "D:\AB\venv\lib\site-packages\selenium\webdriver\common\utils.py", line 150, in keys_to_typing
    for i in range(len(val)):
TypeError: object of type 'ID3TimeStamp' has no len()

我的问题是:mp3 标签有误,还是我做错了什么?

nfo_year 是一个时间戳对象,类型为 ID3TimeStamp。您必须将字符串传递给 AB_author.send_keys。由于 print 有效,您可以尝试 str(nfo_year).