【问题标题】:how can i upload random file using selenium?如何使用硒上传随机文件?
【发布时间】:2021-09-05 11:38:45
【问题描述】:

我没有像示例那样选择特定文件,而是想上传随机文件或从文件资源管理器中选择第一个文件。我该怎么做?

your_file = "/home/you/file.doc"
file_input = driver.find_element_by_xpath('//input[@type="file"]')
file_input.send_keys(your_file)

【问题讨论】:

  • 使用os.listdir获取所有文件的列表并从那里上传任何文件。
  • 我应该如何更改示例代码?它不工作@thethiny
  • 但我希望测试随机选择其中一个文件@thethiny
  • your_file = random.choice(os.listdir('.'))

标签: python selenium testing file-upload


【解决方案1】:

假设您的文件位于“/home/you/”文件夹中,如果这样,您可以执行以下操作:

import os, random
file = random.choice(os.listdir("/home/you/")
file_input = driver.find_element_by_xpath('//input[@type="file"]')
file_input.send_keys(file)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2020-09-27
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多