Home > Back-end >  How can I count the amount of stories I watched using selenium?
How can I count the amount of stories I watched using selenium?

Time:01-14

I'm using python selenium library to write an Instagram Bot. It visits pages of some accounts and watches their stories. I need to count how many stories bot have watched.

Now, bot is just opening story and after that it is sleeping some time. How can I detect using selenium, that for instance first story ended and there is another story. Or that all stories ended?

 def OpenStories(driver):
            storyButton = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CLASS_NAME, '_aarh')))
            ActionChains(driver).move_to_element(storyButton).click(storyButton).click(storyButton).perform()


def WatchStories(driver):
    OpenStories(driver)
    sleep(15)

CodePudding user response:

I have assumed that you click on the account proilfe icon and then try to watch stories til they are all watched.

Once you open the stories, you need to wait for all stories to finish. During this times, you can check for the total stories and watches/unwatchd stories

#This returns the total number of stories for an account
allStories = WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, '_ac3n')))
allStoriesCount = len(allStories)

#This returns all watched and currently being watches stories
watchedStories = WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'div._ac3n div._ac3p')))
watchedStoriesCount = len(watchedStories)

#Unwatched stories can be found using simple subtraction
unWatchedStoriesCount = allStoriesCount - watchedStoriesCount 
  • Related