Get the username of a user from the loaded profile page
(browser, track, logger)
| 1675 | |
| 1676 | |
| 1677 | def get_username(browser, track, logger): |
| 1678 | """Get the username of a user from the loaded profile page""" |
| 1679 | |
| 1680 | query = None |
| 1681 | |
| 1682 | if track == "profile": |
| 1683 | query = "return window._sharedData.entry_data. \ |
| 1684 | ProfilePage[0].graphql.user.username" |
| 1685 | |
| 1686 | elif track == "post": |
| 1687 | query = "return window._sharedData.entry_data. \ |
| 1688 | PostPage[0].graphql.shortcode_media.owner.username" |
| 1689 | |
| 1690 | try: |
| 1691 | username = browser.execute_script(query) |
| 1692 | |
| 1693 | except WebDriverException: |
| 1694 | try: |
| 1695 | browser.execute_script("location.reload()") |
| 1696 | update_activity(browser, state=None) |
| 1697 | |
| 1698 | username = browser.execute_script(query) |
| 1699 | |
| 1700 | except WebDriverException: |
| 1701 | current_url = get_current_url(browser) |
| 1702 | logger.info( |
| 1703 | "Failed to get the username from '{}' page".format( |
| 1704 | current_url or "user" if track == "profile" else "post" |
| 1705 | ) |
| 1706 | ) |
| 1707 | username = None |
| 1708 | |
| 1709 | # in future add XPATH ways of getting username |
| 1710 | |
| 1711 | return username |
| 1712 | |
| 1713 | |
| 1714 | def find_user_id(browser, track, username, logger): |
no test coverage detected