Answer the question
In order to leave comments, you need to log in
Why does Instagram bot on Selenium Python make unsubscribes "once"?
There is a Selenium Python bot, there is an unsubscribe function and it works, but for some reason it skips unsubscribes. It looks something like this:
- the bot loads the profile page and opens the subscriptions tab.
- alternately pokes the unsubscribe buttons until ten people unsubscribe.
- repeats everything again.
while True:
if error_count >= error_max:
break
try:
following_count = browser.find_element(
By.XPATH, '//main/div/header/section/ul/li[3]/a/span').text
print(f"Количество подписок: {following_count}")
count = 10
browser.get(f"https://www.instagram.com/{username}/")
time.sleep(2)
following_button = browser.find_element(By.XPATH, "//li[3]/a")
following_button.click()
time.sleep(2)
# забираем все li из ul, в них хранится кнопка отписки и ссылки на подписки
following_div_block = browser.find_element(By.XPATH, "/html/body/div[6]/div/div/div[3]/ul/div")
following_users = following_div_block.find_elements(By.TAG_NAME, "li")
time.sleep(2)
for user in following_users:
if not count:
time.sleep(sleep_between_iterations)
break
user_url = user.find_element(By.TAG_NAME, "a").get_attribute("href")
user_name = user_url.split("/")[-2]
user.find_element(By.TAG_NAME, "button").click()
time.sleep(random.randrange(min_sleep, max_sleep))
browser.find_element(By.CSS_SELECTOR, "button.-Cab_").click()
print(f"Итерация #{count} >>> Отписался от пользователя {user_name}")
count -= 1
except NoSuchElementException:
error_count += 1
if error_count == error_max:
print(f'''
-----------------------------------------------------------------------------------
----------- Элемент не найден, лимит перезапусков превышен, завершение. -----------
-----------------------------------------------------------------------------------
''')
else:
print(f'''
-----------------------------------------------------------------------------------
----------- Элемент не найден, перезапуск # {error_count}. ------------------------
-----------------------------------------------------------------------------------
''')
time.sleep(30)
continue
Answer the question
In order to leave comments, you need to log in
Try after clicking on the user, after page 43.
add a time.sleep of some kind)
+ On page 44 the absolute xpath is long, many elements, this is not a flexible approach,
it is possible to find a shorter xpath using attributes, for example @id, @class & .. .
So in the same place, run the webdriver locally and see where it sticks
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question