Give upload file path to Instagram with Selenium and python Give upload file path to Instagram with Selenium and python selenium selenium

Give upload file path to Instagram with Selenium and python


If right understand, you are trying to avoid handling with a native window. You can try this:

# get all inputsinputs = driver.find_elements_by_xpath("//input[@accept = 'image/jpeg']").send_keys(os.getcwd() + "/image.png")

Now you can try all of them. I don't know which of them will work.

More about os.getcwd() is here

To be able to perform this code you have to have an element like this:

<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">

EDIT:

It looks like instagram turned of input fields interaction for posts. For Account image it still works, but not for posting. I assume it is was done to prevent bots to post images. Anyway, there is a solution for this problem. You can use AutoIt like this:

import autoitfrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.keys import KeysActionChains(driver).move_to_element( driver.find_element_by_xpath("//path/to/upload/button")).click().perform()handle = "[CLASS:#32770; TITLE:Open]"autoit.win_wait(handle, 60)autoit.control_set_text(handle, "Edit1", "\\file\\path")autoit.control_click(handle, "Button1")


Addition to HumbleFox's answer. To solve his problem regarding the pop-up box not closing or the file pop-up box not closing (bug)

The solution to this is to make the browser headless here's a part of my code for example:

mobile_emulation = { "deviceName": "Pixel 2" }chrome_options = webdriver.ChromeOptions()chrome_options.add_argument('--headless')chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)chrome_options.binary_location = self.opt.binary_locationself.driver = webdriver.Chrome(executable_path=self.path, options=chrome_options)


I think I may have found a solution that works for me. I found if you first have the bot click the plus icon while the browser is in the mobile view.

self.driver.find_element_by_xpath("/html/body/div[1]/section/nav[2]/div/div/div[2]/div/div/div[3]")\        .click()

after that, I would immediately send my file to an input tag in the HTML and I find you may need to play around as to which one works but I find the last input tag worked for me.

self.driver.find_element_by_xpath("/html/body/div[1]/section/nav[2]/div/div/form/input")\        .send_keys("/image/path.jpg")

The one weird thing about this is you will have a popup menu on top of the page but your code will still function with this window displayed over the window you are working on.