Search code examples
pythonhtmlseleniuminstagramweb-deployment

finding correct input tag to upload a image on instagram


i am trying to upload a image to instagram using Python and Selenium... I need to emulate a phone and pass the image in a input tag, it works, because I can upload a profile picture, but the only problem is, that I cant find the correct input tag for uploading a image to my feed. The only one i found was the one for uploading a new profile picture.

Could you please help me to find the input tag?

Here is the code im using:

  def upload(self, image, text):
        driver.get("https://www.instagram.com/")
        #find input field
        upload_btn = driver.find_element(By.XPATH, "needed xpath")
        time.sleep(2)
        #send image
        upload_btn.send_keys("C:\\Users\\brend\PycharmProjects\ClimateCoinBot\\PP2.jpg") #full path of the file which is to be uploaded

Would be cool, if you could help me :)


Solution

  • The element to upload file with .send_keys() method can normally be located with the following locator:

    upload_btn = driver.find_element(By.XPATH, "//input[@type='file']")
    

    You should add a wait / delay after

    driver.get("https://www.instagram.com/")
    

    before

    upload_btn = driver.find_element(By.XPATH, "needed xpath")
    

    to make the page fully loaded before accessing this element.
    So your code can be something like this:

    def upload(self, image, text):
        driver.get("https://www.instagram.com/")
        time.sleep(2)
        #find input field
        upload_btn = driver.find_element(By.XPATH, "//input[@type='file']")
        #send image        
        upload_btn.send_keys("C:\\Users\\brend\PycharmProjects\ClimateCoinBot\\PP2.jpg") 
    

    But it is recommended to use explicit wait rather than hardcoded pauses.