1

I want to download an image from Instagram but I'm unable to click (using code) on the download button of this page

I tried to click using XPath and CSS selector but it doesn't work.

pyautogui.locateOnScreen('image.PNG') does not work most of the times.

elem=driver.find_element_by_xpath
elem('/html/body/div[1]/div[3]/div/div/div[1]/div/div[2]/a').click

driver.find_element_by_css_selector('body > div.page-wrapper > div:nth-child(5) > div > div > div.col-md-3 > div > div.button_div > a').click()

NoSuchElementException

If there's any better way of performing this task please do let me know.

5
  • stackoverflow.com/questions/18439851/… you will get your solution here.(maybe) Commented Aug 22, 2019 at 6:08
  • Add explicit wait Commented Aug 22, 2019 at 6:17
  • @Guy tried still didn't worked Commented Aug 22, 2019 at 6:43
  • @GovindSehrawat is you success to click the picture ? Commented Aug 22, 2019 at 6:44
  • 1
    @Frian check the answer below Commented Aug 22, 2019 at 6:57

1 Answer 1

2

Here you go, you don't need to click any button to download img. If you have the link you can you urllib.request.urlretreive to download files.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import urllib.request

options = Options()
options.add_argument("--headless") # runs chrome in headless mode

driver = webdriver.Chrome(options=options)

driver.get('https://www.sssinstagram.com/p/B1cSJ8hlxXb/')

# To get the link via image displayed on the page use 
src = driver.find_element_by_xpath('/html/body/div[1]/div[3]/div/div/div[1]/div/img').get_attribute('src')
urllib.request.urlretrieve(src, src.split('/')[-1].split('?')[0]) # saves image with name 69346681_776681372728713_6952518582543886663_n.jpg obtained from the image url 

# to get the image via the href available on the page use 
href = driver.find_element_by_xpath('/html/body/div[1]/div[3]/div/div/div[1]/div/div[2]/a').get_attribute('href')
urllib.request.urlretrieve(href, href.split('/')[-1].split('?')[0])

driver.quit()
Sign up to request clarification or add additional context in comments.

6 Comments

thank you so much for the help, can you also explain why it was not clickable
@GovindSehrawat I'm glad it helped.
@GovindSehrawat I was able to click the element. I used the same xpath as yours. Don't know exactly why it didn't work on your side. But for this case, you don't need to click image to get the image.
okay, can you suggest any tutorial or something for learning more about urllib library
@GovindSehrawat What happened ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.