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 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, 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://www.facebook.com" 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) # changes facebook website language to english to use english xpaths if switch_language: links = browser.find_elements_by_xpath('//*[@id="pageFooter"]/ul/li') for link in links: if link.get_attribute('title') == "English (UK)": click_element(browser, Settings, link) web_address_navigator(browser, ig_homepage, Settings) reload_webpage(browser, Settings) # cookie has been LOADED, so the user SHOULD be logged in # check if the user IS logged in login_state = check_authorization(browser, Settings, "https://www.facebook.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 # 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)) # # Check if the first div is 'Create an Account' or 'Log In' # login_elem = browser.find_element_by_xpath( # '//*[@id="email"]' # ) # if login_elem is not None: # try: # (ActionChains(browser) # .move_to_element(login_elem) # .click() # .perform()) # except MoveTargetOutOfBoundsException: # login_elem.click() # # update server calls # update_activity(Settings) # Enter username and password and logs the user in # Sometimes the element name isn't 'Username' and 'Password' # (valid for placeholder too) # wait until it navigates to the login page # login_page_title = "Login" # explicit_wait(browser, "TC", login_page_title, logger) # wait until the 'username' input element is located and visible input_username_XP = '//*[@id="email"]' # 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()) # 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="pass"]') 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()) # 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 = browser.find_element_by_xpath('//*[@type="submit"]') (ActionChains(browser).move_to_element(login_button).click().perform()) # update server calls update_activity(Settings) sleep(1) # dismiss_get_app_offer(browser, logger) # dismiss_notification_offer(browser, logger) if bypass_suspicious_attempt is True: bypass_suspicious_login(browser, bypass_with_mobile) # wait until page fully load # explicit_wait(browser, "PFL", [], logger, 5) # Check if user is logged-in (If there's two 'nav' elements) nav = browser.find_elements_by_xpath('//div[@role="navigation"]') if len(nav) == 2: # create cookie for username print('logged in') pickle.dump( browser.get_cookies(), open('{0}{1}_cookie.pkl'.format(logfolder, username), 'wb')) return True else: return False
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
def verify_action(browser, action, track, username, person, person_id, logger, logfolder): """ Verify if the action has succeeded """ # currently supported actions are follow & unfollow retry_count = 0 if action in ["follow", "unfollow"]: # assuming button_change testing is relevant to those actions only button_change = False if action == "follow": post_action_text_correct = ["Following", "Requested"] post_action_text_fail = ["Follow", "Follow Back", "Unblock"] elif action == "unfollow": post_action_text_correct = ["Follow", "Follow Back", "Unblock"] post_action_text_fail = ["Following", "Requested"] while True: # count retries at beginning retry_count += 1 # find out CURRENT follow status (this is safe as the follow button is before others) following_status, follow_button = get_following_status( browser, track, username, person, person_id, logger, logfolder) if following_status in post_action_text_correct: button_change = True elif following_status in post_action_text_fail: button_change = False else: logger.error( "Hey! Last {} is not verified out of an unexpected " "failure!".format(action)) return False, "unexpected" if button_change: break else: if retry_count == 1: reload_webpage(browser) elif retry_count == 2: # handle it! # try to do the action one more time! click_visibly(browser, follow_button) if action == "unfollow": confirm_unfollow(browser) sleep(4) elif retry_count == 3: 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" if retry_count == 2: logger.info( "Last {} is verified after reloading the page!".format(action)) return True, "success"
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" ig_homepage = "https://www.facebook.com" web_address_navigator(browser, ig_homepage, logger, 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): logger.info("Cookie file not found, creating cookie...") # include time.sleep(1) to prevent getting stuck on google.com time.sleep(1) # changes facebook website language to english to use english xpaths if switch_language: links = browser.find_elements_by_xpath('//*[@id="pageFooter"]/ul/li') for link in links: if link.get_attribute("title") == "English (UK)": click_element(browser, Settings, link) web_address_navigator(browser, ig_homepage, logger, Settings) reload_webpage(browser, Settings) # cookie has been LOADED, so the user SHOULD be logged in # check if the user IS logged in login_state = check_authorization( browser, Settings, "https://www.facebook.com/", username, userid, "activity counts", logger, logfolder, True, ) logger.info("check_authorization: {}".format(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: logger.info( "Issue with cookie for user {}. Creating new cookie...".format( username)) # wait until the 'username' input element is located and visible input_username_XP = '//*[@id="email"]' # explicit_wait(browser, "VOEL", [input_username_XP, "XPath"], logger) input_username = browser.find_element_by_xpath(input_username_XP) logger.info("moving to input_username") logger.info("entering input_username") (ActionChains(browser).move_to_element(input_username).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_password = browser.find_elements_by_xpath('//*[@id="pass"]') if not isinstance(password, str): password = str(password) logger.info("entering input_password") (ActionChains(browser).move_to_element( input_password[0]).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) logger.info("submitting login_button") login_button = browser.find_element_by_xpath('//*[@type="submit"]') (ActionChains(browser).move_to_element(login_button).click().perform()) # update server calls update_activity(Settings) sleep(1) if bypass_suspicious_attempt is True: bypass_suspicious_login(browser, bypass_with_mobile, logger) # Check if user is logged-in (If there's two 'nav' elements) nav = browser.find_elements_by_xpath('//div[@role="navigation"]') if len(nav) == 2: # create cookie for username logger.info("logged in") pickle.dump( browser.get_cookies(), open("{0}{1}_cookie.pkl".format(logfolder, username), "wb"), ) return True else: return False