def check_exists_by_css_selector(webdriver, css_selector): time.sleep(3) try: webdriver.find_element_by_css_selector(css_selector) except NoSuchElementException: return False return True
def watchstories(): numStoriesToWatch = int( input('\033[0;33mHow many stories should we watch?\033[m ') ) # value modifies how many stories you want to watch sleep(1) webdriver.find_element_by_css_selector( '#react-root > section > main > section > div > div.zGtbP.IPQK5.VideM > div > div > div > div > ul > li:nth-child(3) > div > button > div.RR-M-.QN629 > span' ).click() # click on the tab where the stories are sleep(1) stories_watched = 0 sleep(1) try: while (stories_watched != numStoriesToWatch): webdriver.find_element_by_css_selector( '#react-root > section > div.Igw0E.rBNOH.YBx95.vwCYk > div > section > div > button.FhutL > div' ).click() # Clicks next story. stories_watched += 1 print( "\033[0;33mstories watched:\033[m {}".format(stories_watched)) sleep(randint(1, 3)) # Random timer to skip through stories. except KeyboardInterrupt: print('\033[0;33mProgram terminated by the user!\033[m') loadstories = 0 except: print('\033[0;33mNo more stories to view\033[m') loadstories = 0 print( "\033[0;33mFinished watching\033[m {} \033[0;33mstories\033[m".format( stories_watched)) goHome() menu()
def is_exists_element(webdriver, selector): """判断元素是否存在""" try: webdriver.find_element_by_css_selector(selector) return True except: return False
def auto_login(webdriver, login_username, login_password): webdriver.get( 'https://www.instagram.com/accounts/login/?source=auth_switcher' ) # Access Instagram login page sleep(3) # Navigate login infor box username = webdriver.find_element_by_name('username') username.send_keys('') username.send_keys(login_username) password = webdriver.find_element_by_name('password') password.send_keys('') password.send_keys(login_password) # Click log-in button_login = webdriver.find_element_by_css_selector( '#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(4) > button > div' ) button_login.click() sleep(3) # Skip pop-up dialog # Comment these lines out, if you don't get a pop up asking about notifications notnow = webdriver.find_element_by_css_selector( '#react-root > section > main > div > div > div > section > div > button' ) notnow.click() sleep(3) notnow2 = webdriver.find_element_by_css_selector( 'body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.HoLwm' ) notnow2.click() sleep(3)
def login_by_form(self, usr, pswd, webdriver): username = webdriver.find_element_by_id("id_username") password = webdriver.find_element_by_id("id_password") username.send_keys(usr) password.send_keys(pswd) webdriver.find_element_by_css_selector("input[type='submit']").click()
def insta(): import selenium from selenium import webdriver from selenium.webdriver.common.keys import Keys USERNAME = str(input("Enetr your user name:")) PASSWORD = str(input("Enter Your Password:")) chromedriver_path = 'C:/chromedriver.exe' webdriver = webdriver.Chrome(executable_path=chromedriver_path) webdriver.get( 'https://www.instagram.com/accounts/login/?source=auth_switcher') sleep(5) username = webdriver.find_element_by_name('username') username.send_keys(USERNAME) password = webdriver.find_element_by_name('password') password.send_keys(PASSWORD) button_login = webdriver.find_element_by_css_selector( '#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(4) > button > div' ) button_login.click() sleep(6) not_now = webdriver.find_element_by_css_selector( '#react-root > section > main > div > div > div > div > button') not_now.click() sleep(6) notnow = webdriver.find_element_by_css_selector( 'body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.HoLwm' ) notnow.click() def inbox(): button_inbox = webdriver.find_element_by_css_selector( '#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.ctQZg > div > div:nth-child(2) > a > svg' ) button_inbox.click() sleep(10) button_home = webdriver.find_element_by_css_selector( '#react-root > section > div > div._lz6s.Hz2lF > div > div.ctQZg > div > div:nth-child(1) > div > a > svg' ) button_home.click() inbox() sleep(10) def activity(): button_activity = webdriver.find_element_by_css_selector( '#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.ctQZg > div > div:nth-child(4) > a > svg' ) button_activity.click() sleep(2) activity() webdriver.quit()
def _set_race(value, webdriver=None): race_btn = webdriver.find_element_by_css_selector( 'div[id="id_row_race"] button') race_btn.click() # Open race menu for race in value: race_checkbox = webdriver.find_element_by_css_selector( f"div[id='id_row_race'] input[type='checkbox'][value='{race}']") race_checkbox.click() race_btn.click() # Close race menu
def inbox(): button_inbox = webdriver.find_element_by_css_selector( '#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.ctQZg > div > div:nth-child(2) > a > svg' ) button_inbox.click() sleep(10) button_home = webdriver.find_element_by_css_selector( '#react-root > section > div > div._lz6s.Hz2lF > div > div.ctQZg > div > div:nth-child(1) > div > a > svg' ) button_home.click()
def unfollower_func(): try: # CLICKING THE CROSS BUTTON webdriver.find_element_by_css_selector( 'body > div.RnEpo.Yx5HN > div > div > div:nth-child(1) > div > div:nth-child(3) > button > svg').click() unfollow = webdriver.find_element_by_xpath( '/html/body/div[1]/section/main/div/header/section/div[1]/div[2]/span/span[1]/button/div/span') unfollow.click() sleep(5) # CONFIRMING UNFOLLOW webdriver.find_element_by_css_selector( 'body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.-Cab_').click() except NoSuchElementException: # CLICKING THE CROSS BUTTON webdriver.find_element_by_css_selector( 'body > div.RnEpo.Yx5HN > div > div > div:nth-child(1) > div > div:nth-child(3) > button > svg').click() unfollow = webdriver.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/div[1]/button') unfollow.click() sleep(5) # CONFIRMING UNFOLLOW webdriver.find_element_by_css_selector( 'body > div.RnEpo.Yx5HN > div > div > div.mt3GC > button.aOOlW.-Cab_').click()
def likes(): hashtag_list = open("hashtaglist.txt").readlines() tag = -1 goalLikes = int( input( '\033[0;33mHow many likes should we do in each hashtag?:\033[m ')) for hashtag in hashtag_list: currentLikes = 0 tag = tag + 1 print('\033[0;33mLiking the hashtag:\033[m ' + hashtag_list[tag]) webdriver.get('https://www.instagram.com/explore/tags/' + hashtag_list[tag] + '/') image_img = webdriver.find_element_by_xpath( '/html/body/div[1]/section/main/article/div[2]/div/div[1]/div[1]') sleep(1) image_img.click() sleep(1) try: while (currentLikes != goalLikes): sleep(3) image_like_svg = webdriver.find_element_by_css_selector( 'body > div._2dDPU.CkGkG > div.zZYga > div > article > div.eo2As > section.ltpMr.Slqrh > span.fr66n > button > div > span > svg' ) image_like_label = image_like_svg.get_attribute("aria-label") if image_like_label == "Like": image_like_svg.click() currentLikes += 1 print('Liked images: \033[0;33m{}\033[m'.format( currentLikes)) print("Looking for image...") sleep(randint(3, 7)) image_next = webdriver.find_element_by_class_name( 'coreSpriteRightPaginationArrow') image_next.click() elif image_like_label == "Unlike": print('\033[0;33mImage already liked.\033[m') image_next = webdriver.find_element_by_class_name( 'coreSpriteRightPaginationArrow') image_next.click() sleep(randint(1, 4)) else: sleep(5) image_next = webdriver.find_element_by_class_name( 'coreSpriteRightPaginationArrow') image_next.click() sleep(1) except: print( 'Oops, Instagram is having trouble in this tag, lets go to the next one. ' ) image_next = webdriver.find_element_by_class_name( 'coreSpriteRightPaginationArrow') image_next.click() continue print("\033[0;33mFinished liking hashtag:\033[m " + hashtag_list[tag]) sleep(1) print("\033[0;33mFinished liking all hashtags in the hashtag list.\033[m") goHome() sleep(2) menu()
def getinfo(webdriver): ''' 选中老师主页中容纳老师个人信息的标签 :return: 返回一个web-element 对象 ''' try: info = webdriver.find_element_by_css_selector('.t_jbxx_nr') except selenium.common.exceptions.NoSuchElementException: try: info = webdriver.find_element_by_css_selector('.jjside') except selenium.common.exceptions.NoSuchElementException: try: info = webdriver.find_element_by_css_selector('.jbxx') except selenium.common.exceptions.NoSuchElementException: info = webdriver.find_element_by_css_selector('.gdt') return info
def instaLogin(): print("WELCOME TO InstaDownloader by KSHITIJ") print("Navigating to Instagram Login Page") webdriver.get( "https://www.instagram.com/accounts/login/?source=auth_switcher") sleep(2) username = webdriver.find_element_by_name("username") print("Entering Username") username.click() username.send_keys(InstaUsername) password = webdriver.find_element_by_name("password") print("Entering Password") password.click() password.send_keys(InstaPassword) sleep(2) loginButton = webdriver.find_element_by_css_selector( "#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(4) > button > div" ) sleep(1) loginButton.click() print("Logging in . . . ") sleep(2) if (len(webdriver.find_elements_by_class_name('eiCW-')) > 0): print("Incorrect Username or Password") exit()
def instagram_login(self): """ Handles login to instagram and returns the webdriver for use downstream """ webdriver = self.webdriver sleep(2) webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher') sleep(3) try: allow_cookies = webdriver.find_element_by_css_selector('body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.bIiDR') allow_cookies.click() except: pass sleep(3) username = webdriver.find_element_by_name('username') username.send_keys(self.username) password = webdriver.find_element_by_name('password') password.send_keys(self.password) button_login = webdriver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]/button/div') button_login.click() sleep(3) return webdriver
def login_selenium(): '''initiates a chrome_driver window and logs into account''' chromedriver_path = ( '/Users/Beto/galvanize/Instagram-API-python/InstagramAPI/chromedriver') webdriver = webdriver.Chrome(executable_path=chromedriver_path) sleep(2) webdriver.get( 'https://www.instagram.com/accounts/login/?source=auth_switcher') username = webdriver.find_element_by_name('username') username.send_keys('sf_clout') password = webdriver.find_element_by_name('password') password.send_keys('20190101') sleep(10) button_login = webdriver.find_element_by_xpath( '//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[4]/button/div' ) button_login.click() sleep(5) notnow = webdriver.find_element_by_css_selector( 'body > div.RnEpo.Yx5HN > div > div > div.mt3GC > button.aOOlW.HoLwm') notnow.click()
def login(webdriver): # Open the instagram login page webdriver.get( 'https://www.instagram.com/accounts/login/?source=auth_switcher') # sleep for 3 seconds to prevent issues with the server time.sleep(3) # Find username and password fields and set their input using our constants username = webdriver.find_element_by_name('username') username.send_keys('*****@*****.**') password = webdriver.find_element_by_name('password') password.send_keys('Aida72584') # Get the login button try: button_login = webdriver.find_element_by_xpath( '//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[4]/button' ) except: button_login = webdriver.find_element_by_xpath( '//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[6]/button/div' ) # sleep again time.sleep(2) # click login button_login.click() time.sleep(3) # In case you get a popup after logging in, press not now. # If not, then just return try: notnow = webdriver.find_element_by_css_selector( 'body > div.RnEpo.Yx5HN > div > div > div.mt3GC > button.aOOlW.HoLwm' ) notnow.click() except: return
def _perform_submit(webdriver=None): submit_btn = webdriver.find_element_by_css_selector( 'input[type="submit"][name="_save"]') webdriver.execute_script("arguments[0].click();", submit_btn) WebDriverWait(webdriver, 10).until(EC.url_matches(f'{ORIGIN_URL}/patients/(?!new)')) logger.info('Save patient successful')
def notNowNotification(): try: print("Pressing Not Now!!!") notnow = webdriver.find_element_by_css_selector( 'body > div.RnEpo.Yx5HN > div > div > div.mt3GC > button.aOOlW.bIiDR' ) notnow.click() except: print("Maybe there is no Notification PopUp")
def get_profile(username): LoggingIn('username', 'password') #Enter your username/password sleep(5) webdriver.get('https://www.instagram.com/' + username) sleep(3) sleep(2) profile = webdriver.find_element_by_css_selector( ".fAR91.sqdOP.L3NKy._4pI4F._8A5w5" ).click( ) #react-root > section > main > div > header > section > div.Y2E37 > div._862NM > div > button
def load_data(webdriver): for url in URLS: #Get the contents of the URL webdriver.get(url) print(url) # Jialong add # select element by id: webdriver.find_element_by_css_selector("input[type='radio'][name='region'][value='QC']").click() webdriver.find_element_by_id("language-region-set").click() # time.sleep(10) #returns the inner HTML as a string innerHTML = webdriver.page_source #turns the html into an object to use with BeautifulSoup library # print("innerHTML is:") # print(innerHTML) soup = BeautifulSoup(innerHTML, "html.parser") extract_and_load_all_data(soup)
def go_next(self, sleep_time=10): wd = self.wd css = 'a.nav.next.rndBtn.ui_button.primary.taLnk' try: btn = wd.find_element_by_css_selector(css) ActionChains(wd).click(btn).perform() sleep(sleep_time) self.scroll_down() output = False except: output = True return output
def go_next(self, sleep_time=10): wd = self.wd css = 'div#REVIEWS span.nav.next.taLnk' try: btn = wd.find_element_by_css_selector(css) ActionChains(wd).click(btn).perform() sleep(sleep_time) #self.scroll_down() output = False except: output = True return output
def form_data(webdriver): input1 = webdriver.find_element_by_tag_name("input") input1.send_keys("Ivan") input2 = webdriver.find_element_by_name("last_name") input2.send_keys("Petrov") input3 = webdriver.find_element_by_class_name("city") input3.send_keys("Smolensk") input4 = webdriver.find_element_by_id("country") input4.send_keys("Russia") button = webdriver.find_element_by_css_selector("button.btn") button.click() return #form(link, browser)
def LoggingIn (user, passs): username = webdriver.find_element_by_name('username') username.send_keys(user) password = webdriver.find_element_by_name('password') password.send_keys(passs) sleep (3) login_form = webdriver.find_element_by_xpath('//*[@id= "react-root"]/section/main/div/article/div/div[1]/div/form/div[4]/button/div') login_form.click() sleep(3) #Change this to more of a value if you ahve to manually login because of Instagram labelling it as a 'Suspicious attempt' notif = webdriver.find_element_by_css_selector('body > div.RnEpo.Yx5HN > div > div > div.mt3GC > button.aOOlW.HoLwm') notif.click()
def login(): username = webdriver.find_element_by_name('username') username.send_keys(secret.username) password = webdriver.find_element_by_name('password') password.send_keys(secret.password) sleep(2) button_login = webdriver.find_element_by_xpath( '/html/body/div[1]/section/main/div/article/div/div[1]/div/form/div[4]/button' ) button_login.click() sleep(10) try: notnow = webdriver.find_element_by_css_selector( 'button.aOOlW:nth-child(2)') notnow.click( ) # comment these last 2 lines out, if you don't get a pop up asking about notifications except NoSuchElementException: pass
def LoggingIn(user, passs): username = webdriver.find_element_by_name('username') username.send_keys(user) password = webdriver.find_element_by_name('password') password.send_keys(passs) sleep(3) login_form = webdriver.find_element_by_xpath( '//*[@id= "react-root"]/section/main/div/article/div/div[1]/div/form/div[4]/button/div' ) login_form.click() sleep(3) notif = webdriver.find_element_by_css_selector( 'body > div.RnEpo.Yx5HN > div > div > div.mt3GC > button.aOOlW.HoLwm') notif.click()
def find(webdriver, by, css_selector_val): ''' Wrapper function of selenium python to find an elment using locator and locator_value(css_selector_val) Arguments --------- webdriver - object of selenium.webdriver.chrome.webdriver.WebDriver . by - element locator name . contraint: expected value:- id, name, xpath, link_text, partial_link_text, tag_name, class_name, css_selector other value than the expected will return None css_selector_val- value for the element locator i.e. arg 'by' example:- to find all elements with class_name=contact, value for css_selector_val is 'contact' Return --------- Webelement - if the value of arg 'by' is an expected value or None - if the value of arg 'by' is an unexpected value ''' if by == 'id': return webdriver.find_element_by_id(css_selector_val) if by == 'name': return webdriver.find_element_by_name(css_selector_val) if by == 'xpath': return webdriver.find_element_by_xpath(css_selector_val) if by == 'link_text': return webdriver.find_element_by_link_text(css_selector_val) if by == 'partial_link_text': return webdriver.find_element_by_partial_link_text(css_selector_val) if by == 'tag_name': return webdriver.find_element_by_tag_name(css_selector_val) if by == 'class_name': return webdriver.find_element_by_class_name(css_selector_val) if by == 'css_selector': return webdriver.find_element_by_css_selector(css_selector_val) else: return None
def login_code(cachePath, userId, password): global webdriver file = pathlib.Path(cachePath) flag = file.exists() chrome_options = Options() chromedriver_path = '/Users/shaurya/Desktop/Shaurya/pyinstagram/webdriver/chromedriver' chrome_options.add_argument('--user-data-dir={}'.format(cachePath)) if args.headless == 'True': chrome_options.add_argument("--headless") webdriver = webdriver.Chrome(executable_path=chromedriver_path, options=chrome_options) sleep(2) print(flag) if flag == False: print('Cache Doesnot exist') webdriver.get( 'https://www.instagram.com/accounts/login/?source=auth_switcher') sleep(3) webdriver.maximize_window() logger.info("Not Cached: Instagram Site Open") username = webdriver.find_element_by_name('username') username.send_keys(userId) p_word = webdriver.find_element_by_name('password') p_word.send_keys(password) logger.info("Id {}, Password Entered".format(userId)) button_login = webdriver.find_element_by_css_selector( '#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(4)' ) button_login.click() sleep(3) else: webdriver.get('https://www.instagram.com') sleep(3) webdriver.maximize_window() logger.info("Cached: Instagram Site Open") return webdriver
def story_from_feed(): stories_watched = -1 total_stories_watched = 0 webdriver.get('https://www.instagram.com/') sleep(2) try: watch_all_stories_btn = webdriver.find_element_by_xpath( "/html/body/div[1]/section/main/section/div[3]/div[2]/div[1]/a/div" ) watch_all_stories_btn.click() sleep(2) except NoSuchElementException: stories_watched = +limits.max_stories stories_watched += 1 while stories_watched <= limits.max_stories: try: watch_all_stories_next = webdriver.find_element_by_css_selector( ".ow3u_") watch_all_stories_next.click() sleep(randint(2, 3)) stories_watched += 1 total_stories_watched += 1 print("stories watched: {}".format(stories_watched)) except NoSuchElementException: stories_watched += limits.max_stories stories_watched += 1 else: try: watch_all_stories_close = webdriver.find_element_by_xpath( "/html/body/div[1]/section/div/div/section/div[2]/button[3]/div/span" ) watch_all_stories_close.click() except NoSuchElementException: pass sleep(1)
def follow_suggestions(): suggestions_counter = 1 total_suggestion_followed = 0 try: suggestions_all_btn = webdriver.find_element_by_xpath( "/html/body/div[1]/section/main/section/div[3]/div[3]/div[1]/a") suggestions_all_btn.click() sleep(3) while suggestions_counter <= limits.max_follow_suggestion: suggestions_follow_btn = webdriver.find_element_by_css_selector( "div.XfCBB:nth-child({0}) > div:nth-child(3) > button:nth-child(1)" .format(suggestions_counter)) suggestions_follow_btn.click() print("suggestions followed: {}".format(suggestions_counter)) suggestions_counter += 1 total_suggestion_followed += 1 sleep(randint(5, 15)) else: print("total number of users being followed: {}".format( suggestions_counter)) pass except NoSuchElementException: pass
from time import sleep, strftime from random import randint import pandas as pd chromedriver_path = '/usr/bin/chromedriver' # Change this to your own chromedriver path! webdriver = webdriver.Chrome(executable_path=chromedriver_path) sleep(2) webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher') sleep(3) username = webdriver.find_element_by_name('username') username.send_keys('') #specify username password = webdriver.find_element_by_name('password') password.send_keys('') #specify password button_login = webdriver.find_element_by_css_selector( '#react-root > section > main > div > article > div > div > div > form > div > button' ) button_login.click() sleep(3) notnow = webdriver.find_element_by_css_selector( 'body > div.RnEpo.Yx5HN> div > div > div.mt3GC > button.aOOlW.HoLwm') notnow.click( ) #comment these last 2 lines out, if you don't get a pop up asking about notifications hashtag_list = ['fcbarcelona', 'chelsea', 'manchestercity'] prev_user_list = [ ] #- if it's the first time you run it, use this line and comment the two below # prev_user_list = pd.read_csv('20181203-224633_users_followed_list.csv', delimiter=',').iloc[:, # 1:2] # useful to build a user log # prev_user_list = list(prev_user_list['0'])