【问题标题】:Storing image names as a list/array in selenium-python在 selenium-python 中将图像名称存储为列表/数组
【发布时间】:2015-11-16 05:44:58
【问题描述】:

我想将文件路径(图像、视频)列表存储在数组或列表中。 然后我想随机选择一个路径上传。

是否可以使用 selenium web-driver 和 python?

如何从目录中随机获取一个文件?

【问题讨论】:

  • 请重新表述您的问题。您需要发布一些代码并更具体。这个问题太宽泛了。
  • @Yonatan:谢谢。我尝试在使用随机的帮助下做。它奏效了。这是代码:#File common_settings ImgDir=os.path.abspath('.\\resources\\Images') Image_Path, Image_name=CommonFunctions.RandomImageFetch(ImgDir) #File Common_functions def RandomImageFetch(ImgDir): filename = random.choice (os.listdir(ImgDir)) path = os.path.join(ImgDir, filename) return (path, filename)

标签: python python-3.x selenium selenium-webdriver webdriver


【解决方案1】:

这可能因站点而异,但通常应该可行

from random import choice
list_of_files = ['some.jpg', 'thing.jpg']
random_file = choice(list_of_files)

import requests
r = requests.post('http://your-site.org/post', files={random_file: open(random_file, 'rb')})

推荐阅读有关使用 python 请求的“post” - http://docs.python-requests.org/en/latest/user/quickstart/

不建议为此使用硒

【讨论】:

  • 请考虑编辑您的帖子,以添加更多关于您的代码的作用以及它为何能解决问题的说明。一个大部分只包含代码的答案(即使它正在工作)通常不会帮助 OP 理解他们的问题。
【解决方案2】:

我尝试在使用随机数的帮助下进行操作。它奏效了。

代码如下:

文件 common_settings

ImgDir=os.path.abspath('.\\resources\\Images')
Image_Path, Image_name=CommonFunctions.RandomImageFetch(ImgDir)

文件 Common_functions

def RandomImageFetch(ImgDir):        
filename = random.choice(os.listdir(ImgDir))        
path = os.path.join(ImgDir, filename)        
return (path, filename)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    相关资源
    最近更新 更多