【问题标题】:Python: push file to Android device pass but the file is emptyPython:将文件推送到Android设备通行证但文件为空
【发布时间】:2019-06-20 10:45:45
【问题描述】:

我有正在测试的应用程序,我想将file 推送到我的android 设备(真实设备)

这是我尝试过的:

self.driver.push_file('/mnt/sdcard/Pictures/photo.png', r'C:\photo.png')

所以这个操作通过了,我可以在我的device 上看到file,但它的size1kb,当我尝试打开它时,我有这个message

看起来我们不支持这种文件格式

我做错了什么?

【问题讨论】:

  • 你使用什么 api/sdk?
  • 如何查看?
  • 我的意思是你使用的python库/模块
  • 我正在使用 python 3.7 和最新的 selenium
  • 你在使用this吗?

标签: android python appium driver


【解决方案1】:

注意Appium配合Python语言使用,调用self.driver.push_file()方法时,第二个参数是base64文件内容(而不是您机器上文件的路径)。

这意味着您首先必须读取文件,将其转换为 base64(并使用 utf-8 对其进行解码),然后才将其传递给此方法:

import base64
...
with open(r'C:\photo.png','rb') as file:
    driver.push_file('/mnt/sdcard/Pictures/photo.png',
                     base64.b64encode(file.read()).decode('utf-8'))

或者,您可以简单地使用以下命令(仅将 source_path= 添加到您的 sn-p):

self.driver.push_file('/mnt/sdcard/Pictures/photo.png', source_path=r'C:\photo.png')

..因为 push_file() 方法最近更新为支持源路径(参见 Pull #270):

def push_file(self, destination_path, base64data=None, source_path=None)

我当然会推荐 source_path 参数:)

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 2015-12-11
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    相关资源
    最近更新 更多