Search code examples
pythonpython-3.xseleniumpython-webbrowser

Python Uploading on webbrowser


I am writing an script that will upload file from my local machine to an webpage. This is the url: https://tutorshelping.com/bulkask and there is a upload option. but i am trouble not getting how to upload it.

my current script:

import webbrowser, os

def fileUploader(dirname):
    mydir = os.getcwd() + dirname
    filelist = os.listdir(mydir)
    for file in filelist:
        filepath = mydir + file #This is the file absolte file path
        print(filepath)

    url = "https://tutorshelping.com/bulkask"
    webbrowser.open_new(url)  # open in default browser
    webbrowser.get('firefox').open_new_tab(url)


if __name__ == '__main__':
    dirname = '/testdir'
    fileUploader(dirname)

Solution

  • A quick solution would be to use something like AppRobotic personal macro software to either interact with the Windows pop-ups and apps directly, or just use X,Y coordinates to move the mouse, click on buttons, and then to send keyboard keys to type or tab through your files.

    Something like this would work when tweaked, so that it runs at the point when you're ready to click the upload button and browse for your file:

    import win32com.client
    x = win32com.client.Dispatch("AppRobotic.API")
    import webbrowser
    
    # specify URL
    url = "https://www.google.com"
    
    # open with default browser
    webbrowser.open_new(url) 
    
    # wait a bit for page to open
    x.Wait(3000)
    # use UI Item Explorer to find the X,Y coordinates of Search box
    x.MoveCursor(438, 435)
    # click inside Search box
    x.MouseLeftClick
    
    x.Type("AppRobotic")
    x.Type("{ENTER}")