1

I'm working on an Automation Project where I have to upload a file from the local folder, do some tasks and then repeat the same set of tasks by choosing the next file from the local folder. I have about 20 files stored in the local folder. I have successfully completed the work for a single file, but I don't know how to do it for multiple files by choosing them one by one in a sequence. It's my first project in Python, I'm stuck. The code:

def file_upload(self):
    upload_btn = driver.find_element_by_xpath("//") # The Upload Button
    upload_btn.send_keys('File location in the local folder')

def job1(self):
    #set of actions

def job2(self):
    #set of actions

if __name__ == '__main__':
file_upload()
job1()
job2()
Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Humble_boy
  • 1,325
  • 11
  • 19

2 Answers2

1

You would first need to iterate over the files in a directory, for which I'd go for os.listdir(). Then, you need to upload a specific file by sending the absolute path to a file to the "upload" input. Then, it depends on the website and your use case - do you need to do anything extra before uploading the next file, do you stay on the same page or need to navigate back to the "upload" page?

To summarize, you would have something like:

import os  

def file_upload(filename):
    upload_btn = driver.find_element_by_xpath("//") # The Upload Button
    upload_btn.send_keys(filename)

directory_path = '/path/to/your/directory'
for filename in os.listdir(directory_path):
    file_upload(os.path.join(directory_path, filename))

    # get back to the "upload" page here?
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • After a file is uploaded , I have to perform a set of actions for that file, then I have to choose the next file and repeat the same set of actions. This goes on for all the files stored in that directory. There are 20 files for which I have to repeat the same process. – Humble_boy Jan 08 '18 at 06:35
  • For each file in the directory job1() and job2() must be performed. So, the flow is, choose one file perform job1() , job2(). Then choose next file perform job1() and job2(). It goes on for till all the files in the directory have been processed. – Humble_boy Feb 06 '18 at 04:45
  • yes i have to perform job1 and job2 and again navigate back to upload page – Humble_boy Feb 06 '18 at 10:33
1

you can import glob and create a loop for each file

import glob        
def file_upload(self):
    path_of_directory="your path to the files to upload"
    for filename in glob.glob(path_of_directory):
        upload_btn = driver.find_element_by_xpath("//") # The Upload Button
        upload_btn.send_keys(filename)

def job1(self):
    #set of actions

def job2(self):
    #set of actions

if __name__ == '__main__':
file_upload()
job1()
job2()   

edit:

def file_upload(self):
    upload_btn = driver.find_element_by_xpath("//") # The Upload Button
    upload_btn.send_keys('File location in the local folder')

def job1(self):
    #set of actions

def job2(self):
    #set of actions

if __name__ == '__main__':
path_of_directory="your path to the files to upload"
for filename in glob.glob(path_of_directory):               
    file_upload()
    job1()
    job2()
EmreAkkoc
  • 623
  • 1
  • 11
  • 18