selenium webdriver | unfollow all friends at social network

949 views Asked by At

do you have any idea why it unfollow only 15 people instead of all?

You need an account at Instagram to run this script.

for example:
if your account is chrome1 than this script will be working on
https://www.instagram.com/chrome1/following/

for ns in driver.find_elements_by_class_name("_6jvgy"):
    try:
        ns.find_element_by_class_name("_r4e4p").click() # unFollow button!!!

        # time.sleep(2) # the same as without sleep  

        unfollow_nick = ns.find_element_by_class_name("notranslate").get_attribute("title")
        print(unfollow_nick) # now: prints all, but really unfollow only 15. 

    except:
        pass

this question is a continuation of the previous: Scroll in Selenium Webdriver (Python)

all the code needed to run: https://ideone.com/wYjHW4

2

There are 2 answers

9
Naveen Kumar R B On

It is the speed of requests (clicking continuously on Following button) that causing Instagram server to reject/ignore the majority of requests.

Add some sleep time before each request.

In your code, you imported sleep method. so, directly use sleep(2) but not time.sleep, which throws the exception and you are capturing it & bypassed it using pass keyword, so you are not aware of this exception. I suggest print the exception and then use pass keyword.

for ns in driver.find_elements_by_class_name("_6jvgy"):
    try:
        ns.find_element_by_class_name("_r4e4p").click() # unFollow button!!!

        sleep(2) # works now 

        unfollow_nick = ns.find_element_by_class_name("notranslate").get_attribute("title")
        print(unfollow_nick) # now: prints all, but really unfollow only 15. 

    except:
        pass
0
Abhishek Sharma On
self.driver.implicitly_wait(10)
        scroll_boX = self.driver.find_element_by_xpath("/html/body/div[5]/div/div/div[2]/div")
        last_ht, ht = 0, 1
        #last_bt, bt = 1, 1
        while last_ht != ht:
            last_ht = ht
           # last_bt = bt
            sleep(2) 
            ht = self.driver.execute_script("""
                arguments[0].scrollTo(0, arguments[0].scrollHeight); 
                return arguments[0].scrollHeight;
                """, scroll_box)
            sleep(2)