0

Has anyone faced the problem that automation routine for instagram written in python with selenium chromedriver has become difficult to run recently because instagram keeps asking for the code it sends in sms or email?

When you do your normal browser login it asks for the code in sms only once. But when you do it with selenium it asks for it every time.

Here is the code

options = webdriver.ChromeOptions()
        #options.add_argument('headless')
        #options.add_argument('--headless')
        options.add_argument('--disable-logging')
        options.add_argument('--log-level=3')
        driver = webdriver.Chrome(chrome_options=options)
        #driver = webdriver.Chrome()
        print('Driver started successfully!')

        driver.get("https://instagram.com/")    
        time.sleep(6)

        pg=driver.find_element_by_tag_name("html")
        lng=pg.get_attribute("lang")
        #print(lng)
        if lng=='en':
            global lin
            global foll
            global foll_tx
            global subscr_tx
            lin="Log in"
            foll="followers"
            foll_tx="Follow"
            subscr_tx="following"

get_enter_bt

 = driver.find_elements_by_link_text(self.lin)
            lin_found=False
            while not lin_found:
                if len(get_enter_bt)==0:
                    print('Login not found ((( Refreshing...')
                    driver.refresh()
                    time.sleep(6)
                    get_enter_bt = driver.find_elements_by_link_text(self.lin)
                else:
                    lin_found=True
                    print('Login button found!')
            time.sleep(3)
            get_enter_bt[0].click()
            time.sleep(3)

            #login
            login = driver.find_element_by_name("username")
            login.send_keys(username)
            login = driver.find_element_by_name("password")
            login.send_keys(password)
            login.send_keys(Keys.RETURN)

time.sleep(9)

            get_close_mobapp=driver.find_elements_by_css_selector("button._dbnr9")
            if len(get_close_mobapp)!=0:
                get_close_mobapp[0].click()

            notif_switch=driver.find_elements_by_css_selector("button.aOOlW.HoLwm")
            print('notif butt %s' % len(notif_switch))
            if len(notif_switch)>0:
                notif_switch[0].click()
            print(1)
            #detect suspicious login
            susp_login_msg=driver.find_element_by_xpath("//*[@id=\"react-root\"]/section/div/div/div[1]/div/p")#<p class="O4QwN">Подозрительная попытка входа</p>
            print('susm login msg %s' % (susp_login_msg!=None))
            if susp_login_msg:
                if susp_login_msg.text=='Подозрительная попытка входа':
                    try:
                        mobile_button = driver.find_element_by_xpath("//*[@id=\"react-root\"]/section/div/div/div[3]/form/div/div[2]/label")
                        mobile_button.click()
                    except:
                        mobile_button = driver.find_element_by_xpath("//*[@id=\"react-root\"]/section/div/div/div[3]/form/div/div[1]/label")
                        mobile_button.click()
                    snd_code_btn=driver.find_element_by_xpath("//*[@id=\"react-root\"]/section/div/div/div[3]/form/span/button")
                    snd_code_btn.click()

                    print('Instagram detected an unusual login attempt')
                    print('A security code was sent to your mobile '+mobile_button.text)
                    security_code = input('Type the security code here: ')

                    #security_code_field = driver.find_element_by_xpath(("//input[@id='security_code']"))
                    security_code_field.send_keys(security_code)

‐----‐‐‐----

This code works fine, but how to stop instagram from asking for the code in sms every time? Does it detect that I run selenium and runs a kind of antibot activity?

I was running the script on schedule to perform series of likes for my subscribers for example which is time consuming you know and automation was my remedy)))

Shumar
  • 21
  • 3
  • It seems as though you've hit the nail on the head with knowing that they're detecting an automated connection attempt. [Here](https://stackoverflow.com/questions/33225947/can-a-website-detect-when-you-are-using-selenium-with-chromedriver) is a question specifically regarding how to make your chrome look less bot-like – G. Anderson Apr 25 '19 at 16:55
  • their bot detection is working – Corey Goldberg Apr 25 '19 at 23:57
  • @G.Anderson your answer worked well! I've just replaced the "$cdc_" in chromedriver.exe with random string of same length in hex editor and all started to work again. Thanks a lot! – Shumar Apr 26 '19 at 20:23

0 Answers0