Repository: InstaPy/InstaPy

Issue:
Unfollow end prematurelly 
Hello I made the following script, but after it unfollows 19 people it neatly ends the session.
Why is it ending prematurly? 

from instapy import InstaPy
import os
session = InstaPy(username='login', password='pass')
session.login()
session.set_do_follow(enabled=True, percentage=97, times=1)
##session.set_upper_follower_count(limit = 6500)
#session.set_dont_like('nsfw')
#session.set_dont_like('#exactmatch', '[startswith', ']endswith', 'broadmatch')
session.unfollow_users(amount=500)

#session.follow_by_list(accs, times=1)

session.end()


**Here is the CMD line:**

Logged in successfully!
Number of people followed automatically remaining: 776
--> Ongoing Unfollow 1, now unfollowing: b'amar_tafilovic'
--> Ongoing Unfollow 2, now unfollowing: b'super_car_heaven_'
--> Ongoing Unfollow 3, now unfollowing: b'la2natural'
--> Ongoing Unfollow 4, now unfollowing: b'lusinelusarts'
--> Ongoing Unfollow 5, now unfollowing: b'khfitfam'
--> Ongoing Unfollow 6, now unfollowing: b'nicksforzarealty'
--> Ongoing Unfollow 7, now unfollowing: b'kymu.motivation'
--> Ongoing Unfollow 8, now unfollowing: b'roger.despres'
--> Ongoing Unfollow 9, now unfollowing: b'biene141291'
--> Ongoing Unfollow 10, now unfollowing: b'trendynclassy_acce
sleeping for about 10min
--> Ongoing Unfollow 11, now unfollowing: b'lizzy.pg'
--> Ongoing Unfollow 12, now unfollowing: b'chrisprandy'
--> Ongoing Unfollow 13, now unfollowing: b'faits.n.quotes'
--> Ongoing Unfollow 14, now unfollowing: b'mindsetleader'
--> Ongoing Unfollow 15, now unfollowing: b'55totalpackage55'
--> Ongoing Unfollow 16, now unfollowing: b'sorelleshoes'
--> Ongoing Unfollow 17, now unfollowing: b'babymugglemom'
--> Ongoing Unfollow 18, now unfollowing: b'karrcomedy'
--> Ongoing Unfollow 19, now unfollowing: b'roninisgolden'
--> Total people unfollowed : 19

Session ended
-------------


Thank you in advance,
Jeffrey


Discussion:
I think it does not scroll down, I see it im my unfollow, only for followed users "onlyInstapyFollowed=True" so it only check the first page but not scroll down.
I tried it with and without. Same result of it stopping. So I added the unfollow command twice and it did 2 times unfollow 19 people, then ends the session. It does scroll down.

Did try it to see what it does?

It will scroll down if an unfollow user appear on the screen then pressing the unfollow button will scroll down a bit, if  no user show on screen it will stop.
This is the behiver I see, Hope to be fixed soon.

Thank you Sionking for taking the time to try it out also.

Even when I grab the scroll bar and drag it down so that it has a long list on screen, when it reaches 19, it stops and ends the session.

Greetings,
Jeffrey
When I put 3 of these in the script : session.unfollow_users(amount=500)
The script executes in three times, but when I add 5 in total. The script gives command errors. And when I return it back to 3 and execute, the same error keep coming. But once I reduce it back to 1, execute then increase back to 3. It works normal once again.

Maybe someone knows what is going on.
Bear in mind with the following I changed nothing, just kept retyping start.bat
So after the script executing like above for a few times (10-12) 

It logins in then this error below shows in the prompt

**On typing start.bat it gave this error:**

C:\Users\jeffrey\Documents\GitHub\InstaPy>py quickstart.py
Logged in successfully!
Number of people followed automatically remaining: 642
Traceback (most recent call last):
  File "quickstart.py", line 10, in <module>
    session.unfollow_users(amount=500)
  File "C:\Users\jeffrey\Documents\GitHub\InstaPy\instapy\instapy.py", line 530, in unfollow_users
    self.automatedFollowedPool)
  File "C:\Users\jeffrey\Documents\GitHub\InstaPy\instapy\unfollow_util.py", line 52, in unfollow
    dialog = browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div[2]')
  File "C:\Users\jeffrey\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\jeffrey\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line
    'value': value})['value']
  File "C:\Users\jeffrey\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line
    self.error_handler.check_response(response)
  File "C:\Users\jeffrey\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", li
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","select
  (Session info: chrome=60.0.3112.78)
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64)
Now the script doesn't execute anymore. It logins pulls up the unfollow screen, then prints the following on screen before quiting in the command prompt. Just for clarity, I have never tickerd in the code, only added the lines needed in the quickstart.py 

Before the error below the script worked and would execute 19 unfollows and end prematurly, but other than that it worked.

Logged in successfully!
Number of people followed automatically remaining: 642
Traceback (most recent call last):
  File "quickstart.py", line 10, in <module>
    session.unfollow_users(amount=500)
  File "c:\Users\jeffrey\Documents\GitHub\InstaPy\instapy\instapy.py", line 530, in unfollow_users
    self.automatedFollowedPool)
  File "c:\Users\jeffrey\Documents\GitHub\InstaPy\instapy\unfollow_util.py", line 52, in unfollow
    dialog = browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div[2]')
  File "C:\Users\jeffrey\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 313, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\jeffrey\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 791, in find_element
    'value': value})['value']
  File "C:\Users\jeffrey\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "C:\Users\jeffrey\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div/div[2]/div/div[2]"}
  (Session info: chrome=60.0.3112.78)
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64)

I'm now having what seems to be the exact same problem:

Traceback (most recent call last):
  File "C:\Users\Matt\instapy\UNFOLLOW.py", line 13, in <module>
    session.unfollow_users(amount=10, onlyInstapyFollowed = True )
  File "C:\Users\Matt\instapy\instapy\instapy.py", line 530, in unfollow_users
    self.automatedFollowedPool)
  File "C:\Users\Matt\instapy\instapy\unfollow_util.py", line 52, in unfollow
    dialog = browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div[2]')
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 313, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 791, in find_element
    'value': value})['value']
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div/div[2]/div/div[2]"}
  (Session info: chrome=59.0.3071.115)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.15063 x86_64)

Resolving patch:
diff --git a/instapy/unfollow_util.py b/instapy/unfollow_util.py
--- a/instapy/unfollow_util.py
+++ b/instapy/unfollow_util.py
@@ -49,7 +49,7 @@ def unfollow(browser, username, amount, dont_include, onlyInstapyFollowed, autom
 
     # find dialog box
 
-    dialog = browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div[2]')
+    dialog = browser.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div[2]/div/div[2]')
 
     # scroll down the page
     scroll_bottom(browser, dialog, allfollowing)
diff --git a/instapy/util.py b/instapy/util.py
--- a/instapy/util.py
+++ b/instapy/util.py
@@ -21,7 +21,7 @@ def delete_line_from_file(filepath, lineToDelete):
 
 def scroll_bottom(browser, element, range_int):
     # put a limit to the scrolling
-    if range_int > 50: range_int = 1
+    if range_int > 50: range_int = 50
 
     for i in range(int(range_int / 2)):
         browser.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", element)


Test patch: