Search code examples
pythonseleniuminstagram

Locating specific img with Python Selenium


I'm trying to download the post on the Instagram page, but every time Selenium selects and downloads the profile photo.

    def downloadPost(self,link):
     os.system("cls")
     self.link = link
     self.browser = webdriver.Chrome(self.drvPath, chrome_options=self.browserProfile)
     self.browser.get(link)
     time.sleep(2)
     img = self.browser.find_element_by_tag_name('img')
     src = img.get_attribute('src')
     urllib.request.urlretrieve(src, f"{self.imgPath}/igpost.png")
     self.browser.close()

The photo tag I want to capture is under the second img tag and I can't identify it.

html code that I try to scrape


Solution

  • Lots of ways to do this. By grabbing 2nd img tag or the first div with img child class.

    self.browser.find_elements_by_tag_name("img")[1]
    
    self.browser.find_element_by_xpath("//div/img")
    
    self.browser.find_element_by_xpath("//img[@alt='whateverattributevalueithas']")