0

I try to autofollow in Instagram 30 people in a list with xpath and selenium and everybody has the same class, except of them i already followed. When the programm autofollows via class and gets one i already followed it stops. How can i let the program skip the class, if its not the one it is searching?

Thats the statement where it follows the people.

for i in range (1, 31):  
     browser.find_element_by_xpath("//button[@class='sqdOP  L3NKy   y3zKF     ']").click()
     print(i)

I already tried this but that also wont work

def follower():
    for i in range (1, 31):
        try:
            browser.find_element_by_xpath("//button[@class='sqdOP  L3NKy   y3zKF     ']").click()
            print(i)
        except:
            continue

You can try it yourself, dont forget to change the login information (Line 12&13)

from selenium import webdriver
from time import sleep

browser = webdriver.Chrome()

url = "https://www.instagram.com/"
browser.get(url)

sleep(1)
browser.find_element_by_xpath("/html/body/div[2]/div/div/div/div[2]/button[1]").click()
#Login
browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input").send_keys("Username")
browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input").send_keys("Password")
browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button").click()
sleep(3)

browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div/div/div/button").click()
browser.find_element_by_xpath("/html/body/div[4]/div/div/div/div[3]/button[2]").click()
url = "https://www.instagram.com/scarlxrd/"
browser.get(url)
browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div[3]/article/div/div/div[1]/div[1]/a/div/div[2]").click()#Bildwahl
sleep(1)
try: 
    browser.find_element_by_xpath("/html/body/div[5]/div[2]/div/article/div[3]/section[2]/div/div[2]/a").click()
except:
    browser.find_element_by_xpath("/html/body/div[5]/div[2]/div/article/div[3]/section[2]/div/div/a").click()
sleep(1)

for i in range (1, 31):
        try:
            browser.find_element_by_xpath("//button[@class='sqdOP  L3NKy   y3zKF     ']").click()
            print(i)
Samet
  • 33
  • 1
  • 6

1 Answers1

0

You can try something like this:

# Create a range that does not contain 50
for i in [x for x in xrange(100) if x != 50]:
    print i

# Create 2 ranges [0,49] and [51, 100] (Python 2)
for i in range(50) + range(51, 100):
    print i

# Create a iterator and skip 50
xr = iter(xrange(100))
for i in xr:
    print i
    if i == 49:
        next(xr)

# Simply continue in the loop if the number is 50
for i in range(100):
    if i == 50:
        continue
    print i

please see Skip over a value in the range function in python