def verify_action(browser, action, track, username, person, person_id, logger, logfolder): """ Verify if the action has succeeded """ # currently supported actions are follow & unfollow if action in ["follow", "unfollow"]: if action == "follow": post_action_text = "//button[text()='Following' or text(" ")='Requested']" elif action == "unfollow": post_action_text = "//button[text()='Follow' or text()='Follow " "Back']" button_change = explicit_wait(browser, "VOEL", [post_action_text, "XPath"], logger, 7, False) if not button_change: reload_webpage(browser, Settings) following_status, follow_button = get_following_status( browser, track, username, person, person_id, logger, logfolder) # find action state *.^ if following_status in ["Following", "Requested"]: action_state = False if action == "unfollow" else True elif following_status in ["Follow", "Follow Back"]: action_state = True if action == "unfollow" else False else: action_state = None # handle it! if action_state is True: logger.info( "Last {} is verified after reloading the page!".format( action)) elif action_state is False: # try to do the action one more time! click_visibly(browser, Settings, follow_button) if action == "unfollow": sleep(4) # TODO: use explicit wait here confirm_unfollow(browser) button_change = explicit_wait(browser, "VOEL", [post_action_text, "XPath"], logger, 9, False) if not button_change: logger.warning("Phew! Last {0} is not verified." "\t~'{1}' might be temporarily blocked " "from {0}ing\n".format(action, username)) sleep(210) return False, "temporary block" elif action_state is None: logger.error( "Hey! Last {} is not verified out of an unexpected " "failure!".format(action)) return False, "unexpected" return True, "success"
def get_following_status(browser, track, username, person, person_id, logger, logfolder): """ Verify if you are following the user in the loaded page """ if track == "profile": ig_homepage = "https://www.medium.com/" web_address_navigator(browser, ig_homepage + person, Settings) follow_button_XP = ( '//*[@id="page-container"]/div/div/ul/li/div/div/span/button[@type="button"]/span[text()="Follow"]' ) failure_msg = "--> Unable to detect the following status of '{}'!" # user_inaccessible_msg = ( # "Couldn't access the profile page of '{}'!\t~might have changed the" # " username".format(person)) # check if the page is available # valid_page = is_page_available(browser, logger, Settings) # if not valid_page: # logger.warning(user_inaccessible_msg) # person_new = verify_username_by_id(browser, # username, # person, # None, # logger, # logfolder) # if person_new: # web_address_navigator( browser, ig_homepage + person_new, Settings) # valid_page = is_page_available(browser, logger, Settings) # if not valid_page: # logger.error(failure_msg.format(person_new.encode("utf-8"))) # return "UNAVAILABLE", None # else: # logger.error(failure_msg.format(person.encode("utf-8"))) # return "UNAVAILABLE", None # wait until the follow button is located and visible, then get it follow_button = explicit_wait(browser, "VOEL", [follow_button_XP, "XPath"], logger, 7, False) logger.info("follow_button = {}".format(follow_button)) if not follow_button: browser.execute_script("location.reload()") update_activity(Settings) follow_button = explicit_wait(browser, "VOEL", [follow_button_XP, "XPath"], logger, 14, False) logger.info("follow_button retried = {}".format(follow_button)) if not follow_button: # cannot find the any of the expected buttons logger.error(failure_msg.format(person.encode("utf-8"))) return None, None # get follow status following_status = follow_button.text logger.info("following_status returned = {}".format(following_status)) return following_status, follow_button
def get_friending_status(browser, track, username, person, person_id, logger, logfolder): """ Verify if you are friending the user in the loaded page """ if track == "profile": ig_homepage = "https://www.facebook.com/" web_address_navigator( browser, ig_homepage + person, Settings) friend_button_XP = ("//div[@id='fbTimelineHeadline']/div/div/div/div/button[@type='button'][text()='Add Friend']") failure_msg = "--> Unable to detect the friending status of '{}'!" user_inaccessible_msg = ( "Couldn't access the profile page of '{}'!\t~might have changed the" " username".format(person)) # check if the page is available valid_page = is_page_available(browser, logger, Settings) if not valid_page: logger.warning(user_inaccessible_msg) person_new = verify_username_by_id(browser, username, person, None, logger, logfolder) if person_new: web_address_navigator( browser, ig_homepage + person_new, Settings) valid_page = is_page_available(browser, logger, Settings) if not valid_page: logger.error(failure_msg.format(person_new.encode("utf-8"))) return "UNAVAILABLE", None else: logger.error(failure_msg.format(person.encode("utf-8"))) return "UNAVAILABLE", None # wait until the friend button is located and visible, then get it friend_button = explicit_wait(browser, "VOEL", [friend_button_XP, "XPath"], logger, 7, False) if not friend_button: browser.execute_script("location.reload()") update_activity(Settings) friend_button = explicit_wait(browser, "VOEL", [friend_button_XP, "XPath"], logger, 14, False) if not friend_button: # cannot find the any of the expected buttons logger.error(failure_msg.format(person.encode("utf-8"))) return None, None # get friend status friending_status = friend_button.text return friending_status, friend_button
def login_user(browser, username, userid, password, logger, logfolder, switch_language=True, bypass_suspicious_attempt=False, bypass_with_mobile=False): """Logins the user with the given username and password""" assert username, 'Username not provided' assert password, 'Password not provided' print(username, password) ig_homepage = "https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fhl%3Den%26feature%3Dcomment%26app%3Ddesktop%26next%3D%252Fall_comments%253Fv%253DLAr6oAKieHk%26action_handle_signin%3Dtrue&uilel=3&service=youtube&passive=true&hl=en" web_address_navigator(browser, ig_homepage, Settings) cookie_loaded = False # try to load cookie from username try: for cookie in pickle.load( open('{0}{1}_cookie.pkl'.format(logfolder, username), 'rb')): browser.add_cookie(cookie) cookie_loaded = True except (WebDriverException, OSError, IOError): print("Cookie file not found, creating cookie...") # include time.sleep(1) to prevent getting stuck on google.com time.sleep(1) web_address_navigator(browser, ig_homepage, Settings) reload_webpage(browser, Settings) # try: # profile_pic = browser.find_element_by_xpath('//header/div[8]/details/summary/img') # if profile_pic: # login_state = True # else: # login_state = False # except Exception as e: # print(e) # login_state = False # print('login_state:', login_state) # if login_state is True: # # dismiss_notification_offer(browser, logger) # return True # if user is still not logged in, then there is an issue with the cookie # so go create a new cookie.. if cookie_loaded: print("Issue with cookie for user {}. Creating " "new cookie...".format(username)) # wait until the 'username' input element is located and visible input_username_XP = '//*[@id="identifierId"]' # explicit_wait(browser, "VOEL", [input_username_XP, "XPath"], logger) input_username = browser.find_element_by_xpath(input_username_XP) print('moving to input_username') print('entering input_username') (ActionChains(browser).move_to_element(input_username).click().send_keys( username).perform()) sleep(1) (ActionChains(browser).send_keys(Keys.ENTER).perform()) # update server calls for both 'click' and 'send_keys' actions for i in range(2): update_activity(Settings) sleep(1) # password input_password = browser.find_elements_by_xpath( "//*[@id='password']/div[1]/div/div[1]/input") if not isinstance(password, str): password = str(password) print('entering input_password') (ActionChains(browser).move_to_element( input_password[0]).click().send_keys(password).perform()) sleep(1) (ActionChains(browser).send_keys(Keys.ENTER).perform()) # update server calls for both 'click' and 'send_keys' actions for i in range(2): update_activity(Settings) sleep(1) print('submitting (ie just pres enter)') (ActionChains(browser).send_keys(Keys.ENTER).perform()) # update server calls update_activity(Settings) sleep(1) # wait until page fully load explicit_wait(browser, "PFL", [], logger, 5) try: profile_pic = browser.find_element_by_xpath('//*[@id="img"]') if profile_pic: login_state = True print('logged in') pickle.dump( browser.get_cookies(), open('{0}{1}_cookie.pkl'.format(logfolder, username), 'wb')) else: login_state = False except Exception as e: print(e) login_state = False return login_state
def login_user(browser, username, password, userid, logger, logfolder): """Logins the user with the given username and password""" assert username, 'Username not provided' assert password, 'Password not provided' print(username, password) homepage = "https://www.medium.com/" web_address_navigator(browser, homepage, Settings) cookie_loaded = False # try to load cookie from username try: for cookie in pickle.load( open('{0}{1}_cookie.pkl'.format(logfolder, username), 'rb')): browser.add_cookie(cookie) cookie_loaded = True except (WebDriverException, OSError, IOError): print("Cookie file not found, creating cookie...") # include time.sleep(1) to prevent getting stuck on google.com time.sleep(1) web_address_navigator(browser, homepage, Settings) reload_webpage(browser, Settings) # if user is still not logged in, then there is an issue with the cookie # so go create a new cookie.. # cookie has been LOADED, so the user SHOULD be logged in # check if the user IS logged in if cookie_loaded: login_state = check_authorization(browser, Settings, "https://www.medium.com", username, userid, "activity counts", logger, logfolder, True) print('check_authorization:', login_state) if login_state is True: # dismiss_notification_offer(browser, logger) return True else: print("Issue with cookie for user {}. Creating " "new cookie...".format(username)) input_username_XP = '//div[2]/div[1]/input[@name="email"]' input_usernames = browser.find_elements_by_xpath( input_username_XP) #TODO : Two tags found just take the last one print('moving to input_username') print('entering input_username: {}'.format(username)) #email login doesn't reprompt (ActionChains(browser).move_to_element( input_usernames[-1]).click().send_keys(username).perform()) # update server calls for both 'click' and 'send_keys' actions for i in range(2): update_activity(Settings) sleep(1) # password input_passeord_XP = '//div[2]/div[2]/input[@name="password"]' input_passwords = browser.find_elements_by_xpath(input_passeord_XP) print('entering input_password') (ActionChains(browser).move_to_element( input_passwords[-1]).click().send_keys(password).perform()) # update server calls for both 'click' and 'send_keys' actions for i in range(2): update_activity(Settings) sleep(1) print('submitting login_button') login_button_XP = '//div[2]/div[3]/input' login_button = browser.find_element_by_xpath(login_button_XP) (ActionChains(browser).move_to_element(login_button).click().perform()) # update server calls update_activity(Settings) sleep(2) # wait until page fully load explicit_wait(browser, "PFL", [], logger, 5) # Check if user is logged-in (If there's two 'nav' elements) login_state = check_authorization(browser, Settings, "https://www.medium.com/login", username, userid, "activity counts", logger, logfolder, True) print('check_authorization again:', login_state) return login_state