def search_hashtag(hashtag: str): try: search_field = driver.find_element_by_xpath( '//input[@placeholder="Search"]') search_field.clear() search_field.send_keys(os.path.join("#"), hashtag) sleep(2) search_results = driver.find_element_by_xpath( "//a[contains(@href,'/{}')]".format(hashtag)) search_results.click() print(f"Opening {hashtag} hashtag page ...") sleep(4) except StaleElementReferenceException: # pragma: no cover pass
def home_page(): # pragma: no cover # Instagram Home Page - no real need at this time to use home_icon = driver.find_element_by_xpath( '//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[1]/div/a' ) home_icon.click() print("Loading home page...") sleep(2)
def new_message(): # pragma: no cover # To send a new DM new_dm_icon = driver.find_element_by_xpath( '//*[@id="react-root"]/section/div/div[2]/div/div/div[1]/div[1]/div/div[3]/button' ) new_dm_icon.click() print("Creating new message...") sleep(2)
def inbox(): # pragma: no cover # Your inbox inbox_icon = driver.find_element_by_xpath( '//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[2]/a' ) inbox_icon.click() print("Loading inbox page...") sleep(2)
def explore_page(): # pragma: no cover # Leads to explore page - no real need at this time to use explore_icon = driver.find_element_by_xpath( '//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[3]/a' ) explore_icon.click() print("Loading explore page...") # This page take longer to load sleep(4)
def follow(): """ To follow an account while on their profile""" # Click follow button on profile page # Only works if you are not following the profile already follow_button = driver.find_element_by_xpath( "//button[contains(text(), 'Follow')]") follow_button.click() print("Started following account...") sleep(2)
def close_followers_window(): # pragma: no cover # Click on the cross to close the followers popup window try: close_button = driver.find_element_by_xpath( "/html/body/div[5]/div/div/div[1]/div/div[2]/button") print("Closing followers popup...") close_button.click() sleep(2) except NoSuchElementException: pass
def close_post(): # pragma: no cover # Click on the cross to close the post/picture # close_button = driver.find_element_by_class_name("wpO6b") # Error with the class try: close_button = driver.find_element_by_xpath( "/html/body/div[5]/div[3]/button") print("Closing post...") close_button.click() sleep(2) except NoSuchElementException: pass
def your_account_page(account: str): # Open your own account page using the drop down menu - more human-like try: your_acct = driver.find_element_by_xpath( "//a[contains(@href,'/{}')]".format( os.environ.get("username"))) your_acct.click() sleep(2) except NoSuchElementException: Pages.account_page(account)
def unfollow(): """ To unfollow an account while on their profile""" # Click on icon with icon user and check mark following_icon = driver.find_element_by_class_name("yZn4P") following_icon.click() sleep(2) # Popup opens and click unfollow unfollow_button = driver.find_element_by_xpath( "//button[contains(text(), 'Unfollow')]") unfollow_button.click() sleep(2)
def search_general(keyword: str): try: search_field = driver.find_element_by_xpath( '//input[@placeholder="Search"]') search_field.clear() search_field.send_keys(keyword) sleep(2) search_field.send_keys(Keys.RETURN) sleep(2) search_field.send_keys(Keys.RETURN) sleep(4) except StaleElementReferenceException: # pragma: no cover pass
def get_followers(): # pragma: no cover """ To get the full list of followers from an account """ view_followers = driver.find_element_by_xpath( "//a[contains(@href,'/followers')]") view_followers.click() print("Checking list of followers...") sleep(2) # Follower popup # I noticed that this xpath sometimes fluctuates which broke the code so had to consider that # This is not a normal behavior for an xpath... But this below fix the issue try: follower_popup = driver.find_element_by_xpath( "/html/body/div[4]/div/div/div[2]") except NoSuchElementException: # pragma: no cover follower_popup = driver.find_element_by_xpath( "/html/body/div[5]/div/div/div[2]") # Scrolling until the end of the popup end, beginning = 0, 1 while end != beginning: end = beginning sleep(1) try: beginning = driver.execute_script( "arguments[0].scrollTo(0, arguments[0].scrollHeight); return arguments[0].scrollHeight;", follower_popup, ) except StaleElementReferenceException: # pragma: no cover continue # Find all username elements ele_user = follower_popup.find_elements_by_tag_name("a") usernames = [username.text for username in ele_user if username.text != ""] # Closing popup close_followers_window() return usernames
def post_comment(your_comment: str): """ Function that automatically add comments :param your_comment: str :return: 1 to use for counting posted comments :rtype: int """ # Click on the text area to add a comment add_comment = driver.find_element_by_xpath( "//textarea[contains(@placeholder, 'Add a comment')]") add_comment.click() # Type your comment add_comment = driver.find_element_by_xpath( "//textarea[contains(@placeholder, 'Add a comment')]") print("Writing comment...") add_comment.send_keys(your_comment) sleep(1) # Press enter in order to submit comment add_comment.send_keys(Keys.RETURN) print("Comment posted :)") sleep(3) return 1
def search_account(account: str): try: search_field = driver.find_element_by_xpath( '//input[@placeholder="Search"]') search_field.clear() search_field.send_keys(os.path.join("@"), account) sleep(2) search_field.send_keys(Keys.RETURN) sleep(2) search_field.send_keys(Keys.RETURN) print(f"Opening {account} profile's page ...") sleep(4) except StaleElementReferenceException: # pragma: no cover pass
def get_vid(): # Find the first element containing video # The first element will always be the post's video page_video = driver.find_element_by_xpath("//video") # Getting the URL of the video # url = page_video.get_attribute("src") # On December 8th Instagram changed something here which affected src that we were getting # Due to this and for now we will only save the first frame of the video as jpg - speed up bot url = page_video.get_attribute("poster") # Parsing the URL in order to get the filename path get_filename = urlparse(url).path # Video filename filename = os.path.basename(get_filename) # Create folders to store the video os.makedirs("data/videos", exist_ok=True) # Path where the video will be saved path = os.path.join("data/videos/", filename) # Making sure the video doesn't exist already if not os.path.exists(path): # Using package requests to check for any http issue like 4XX or 5XX errors with requests.get(url, stream=True) as req: # Checking if request is successful (None = no error) if req.raise_for_status() is not None: # pragma: no cover print("Error: URL of video is incorrect :(") pass # Writing file atomically locally with atomic_write(path, as_file=False) as f: urlretrieve(url, filename=f) print("Video saved :)") # Returning the video path in order to use with other functions return path else: # Pass if the video is already present in the folder print("Video already exists :(") return path