Check if selenium instagram post is a video

I have a bot that loops over all user posts and get the number of likes and it’s link. The problem is that the code breaks when it encounters a video.

def usermostlikedposts(self, username, nofposts):         self.nav_user(username)         for i in range(nofposts):             try:                 post = self.browser.find_element_by_xpath('(//div[@class=\'eLAPa\']//parent::a)[{}]'.format(i+1))                 post.click()                 WebDriverWait(self.browser, 10).until(EC.presence_of_element_located((By.XPATH , '/html/body/div[4]/div[2]/div/article/div[3]/section[2]/div/div/button/span')))                 likes = self.browser.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[2]/div/div/button/span').get_attribute("innerHTML")                 link = post.get_attribute("href")                 self.likesposts[likes] = link                 x = self.browser.find_element_by_xpath('/html/body/div[4]/div[3]/button')                 x.click()             except:                 pass 

.

I don’t know how to make this work on videos, i want to make the bot get the number of views instead of likes whenever it encounters a video, but i fail to implement this. I’m looking for a way to check if the post is a video, and write the code according to that condition

Add Comment
1 Answer(s)

I managed to solve it:

def usermostlikedposts(self, username, nofposts):         self.nav_user(username)         for i in range(nofposts):             try:                 post = self.browser.find_element_by_xpath('(//div[@class=\'eLAPa\']//parent::a)[{}]'.format(i+1))                 post.click()                 WebDriverWait(self.browser, 10).until(EC.presence_of_element_located((By.XPATH , '/html/body/div[4]/div[2]/div/article/div[3]/section[1]/span[3]/button')))                 try:                     likes = self.browser.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[2]/div/div/button/span').get_attribute("innerHTML")                     link = post.get_attribute("href")                     self.likesposts[likes] = link                 except:                     pass                 x = self.browser.find_element_by_xpath('/html/body/div[4]/div[3]/button')                 x.click()             except:                 pass 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.