当传递给 selenium 时,从命令捕获的标准输出有两倍的换行符

stdout captured from command has twice as many newlines when passed to selenium

我有一些代码试图捕获标准输出:

def MediaInfo():
    cmd= ['MediaInfo.exe', 'videofile.mkv']
    test = subprocess.run(cmd, capture_output=True)
    info = test.stdout.decode("utf-8")
    print (info)

使用print或写入文件时,看起来不错。但是当我使用 selenium 将其填充到消息框时:

techinfo = driver.find_element(By.NAME, "techinfo").send_keys(info)

每行之间多了一个空行。最初我有一个问题,其中 stdout 是一个字节文字。看起来 b"This is the first line.\r\nThis is the second line.\r\n" 添加 .decode("utf-8") 是解决问题的方法,但我想知道在某些情况下是否有人将 \r\n 解释为创建两条线。我只是不确定这是否是 Selenium 或 subprocess 或其他问题。 Selenium 写入的网页元素似乎没有问题。如果我从文本文件中复制并粘贴它,它看起来是正确的。意思是,这不仅仅是它的显示方式,实际上有两倍多的换行符。有任何想法吗?我不想只是循环并删除多余的行。太笨拙了。根据我的阅读,我猜这是 Python 3 的问题。

send_keys() 将单独发送每个键,这意味着“\r\n”作为两次按键发送。在发送到元素之前用“\n”替换“\r\n”应该可以解决问题。