Get the list of users from the 'Likes' dialog of a photo
(browser, amount=20, logger=None)
| 325 | |
| 326 | |
| 327 | def likers_from_photo(browser, amount=20, logger=None): |
| 328 | """Get the list of users from the 'Likes' dialog of a photo""" |
| 329 | |
| 330 | try: |
| 331 | if check_exists_by_xpath( |
| 332 | browser, read_xpath(likers_from_photo.__name__, "second_counter_button") |
| 333 | ): |
| 334 | liked_this = browser.find_elements( |
| 335 | By.XPATH, |
| 336 | read_xpath(likers_from_photo.__name__, "second_counter_button"), |
| 337 | ) |
| 338 | element_to_click = liked_this[0] |
| 339 | elif check_exists_by_xpath( |
| 340 | browser, read_xpath(likers_from_photo.__name__, "liked_counter_button") |
| 341 | ): |
| 342 | liked_this = browser.find_elements( |
| 343 | By.XPATH, read_xpath(likers_from_photo.__name__, "liked_counter_button") |
| 344 | ) |
| 345 | likers = [] |
| 346 | |
| 347 | for liker in liked_this: |
| 348 | if " like this" not in liker.text: |
| 349 | likers.append(liker.text) |
| 350 | |
| 351 | if " others" in liked_this[-1].text: |
| 352 | element_to_click = liked_this[-1] |
| 353 | |
| 354 | elif " likes" in liked_this[0].text: |
| 355 | element_to_click = liked_this[0] |
| 356 | |
| 357 | else: |
| 358 | logger.info( |
| 359 | "Few likes, not guaranteed you don't follow these" |
| 360 | " likers already.\nGot photo likers: {}".format(likers) |
| 361 | ) |
| 362 | return likers |
| 363 | |
| 364 | else: |
| 365 | # If you are here, it is because you discovered that sometimes an |
| 366 | # xpath does not work, at the moment the number of views in a video |
| 367 | # does not behave like the number of likes in a photo. Clicking on |
| 368 | # the number of views only shows the same number in a floating |
| 369 | # window; no users to get. |
| 370 | # www.instagram.com/p/CASprIDgOOO/ -> 3,275,561 views -> 11-24-20 |
| 371 | logger.info("Couldn't find liked counter button. May be a video.") |
| 372 | logger.info("Trying again for some image, moving on...") |
| 373 | return [] |
| 374 | |
| 375 | sleep(1) |
| 376 | click_element(browser, element_to_click) |
| 377 | logger.info("Opening likes...") |
| 378 | |
| 379 | # update server calls |
| 380 | update_activity(browser, state=None) |
| 381 | sleep(1) |
| 382 | |
| 383 | # get a reference to the 'Likes' dialog box |
| 384 | dialog = browser.find_element( |
no test coverage detected