Now when story is a real hot trend, and we also want to avoid act like a bot, we should add story watching.
Who is willing to implement it ?
I did most parts, but struggling xpath for close button .
@sionking, try with its css selector,
find_element_by_css_selector("button._t848o > div > span")
Ok I am done implementing will PR soon.
def watch_the_story(browser, logger):
# xpaths
prev_xpath = "//button/div[@class='coreSpriteRightChevron']"
next_xpath = "//button/div[@class='coreSpriteRightChevron']"
close_xpath = "//button/div[@class='coreSpriteCloseLight']"
prev_elem = None
next_elem = None
try:
# check if there is a story for this user according to IG user data
is_story = browser.execute_script(
"return window._sharedData.entry_data."
"ProfilePage[0].graphql.user.has_highlight_reel")
if is_story:
# first img is a story
first_imag = browser.find_element_by_tag_name("img")
click_element(browser, first_imag)
sleep(2)
# find the buttons before story ends to save time on xpath fail
close_elem = browser.find_element_by_xpath(close_xpath)
while random.randint(1,5) != 1:
# find the element
if next_elem is None:
next_elem = browser.find_element_by_xpath(next_xpath)
sleep(random.randint(2,6))
# move to the right (next)
click_element(browser, next_elem)
sleep(2) # let the prev button load
if random.randint(1, 10) == 1:
if prev_elem is None:
prev_elem = browser.find_element_by_xpath(prev_xpath)
sleep(random.randint(2, 4))
# play with it, we can go back
click_element(browser, prev_elem)
sleep(5)
# close button since this is a long story..
click_element(browser, close_elem)
except NoSuchElementException:
# story is finished
pass
except StaleElementReferenceException:
# story is finished
pass
except WebDriverException:
logger.error('has_highlight_reel not exist')
except:
logger.error('Story: something went wrong')
It do have some timing issues, considering the page moves alot :()
I need to make sure we are no longer in the story when we are out of this function.
@sionking
Multiple blocks of catching different exceptions is amazing, nice work!
The documentation is not about this. Was there a PR?
And is it possible to add comments in the stories?
Most helpful comment
The documentation is not about this. Was there a PR?
And is it possible to add comments in the stories?