0

I'm coding a script that set likes on instagram photos, but after some likes (once 14, once 50, once 64), the shell gives me this error:

in <module> ui.WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".dCJp8.afkep.coreSpriteHeartOpen._0mzm-"))).click() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) TimeoutException: Message:

I understand that the problem should be in webdriver wait,but I don't know how to improve my script.

this is my script:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome(executable_path = '/usr/local/bin/chromedriver')
driver.implicitly_wait(30)
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")

#log in 
time.sleep(1)
usm = driver.find_element_by_name('username').send_keys("givcomomvrvglino")
pwd = driver.find_element_by_name('password').send_keys("x")
btnLog = driver.find_element_by_tag_name('form').submit()

#disattiva le notifiche
ui.WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".aOOlW.HoLwm"))).click()

tag = "likeforfollow"

url_to_search = "https://www.instagram.com/explore/tags/" + tag + "/"

driver.get(url_to_search)

contatore = 0

for i in range(500):
  if i == 0:
        ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".v1Nh3.kIKUG._bz0w"))).click()
        ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".dCJp8.afkep.coreSpriteHeartOpen._0mzm-"))).click()
        ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".HBoOv.coreSpriteRightPaginationArrow"))).click()

  else:
        ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".dCJp8.afkep.coreSpriteHeartOpen._0mzm-"))).click()
        ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".HBoOv.coreSpriteRightPaginationArrow"))).click()

  contatore = contatore + 1

  print(contatore)

  time.sleep(3)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

As you mentioned shell gives this error sometimes after some likes (once 14, once 50, once 64) so virtually there seems to be no definite/specific error as such.

If you look at the DOM Tree of Instagram:

  • The css contains reference to react-root

    <style type="text/css" data-isostyle-id="786433">#react-root,article,div,footer,header,main,nav,section{-webkit-box-align:stretch;-webkit-align-items:stretch;-ms-flex-align:stretch;align-items:stretch;border:0 solid #000;-webkit-box-sizing:border-box;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;margin:0;padding:0;position:relative}</style>
    
  • The <body> starts with a <span> tag with contains the react-root as follows:

    <span id="react-root">
    

So as your Locator Strategies are based on those dynamic classes e.g. v1Nh3, kIKUG, _bz0w, _0mzm- etc, the search for the elements are bound to fail anytime either too early or a bit later during your Test Execution.


Solution

A propersolution would be to use Locating Strategies based on satic elements present within the HTML DOM.

As an example to click() on the element with text as Not now on Instagram popup notification you can use the following solution:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Not Now')]"))).click()

You can find a detailed discussion in Click ''Not now" on Instagram notifications ask using Selenium and Python

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352