0

I am building an Instagram bot using selenium, and I want to click on my followers to open the complete list of followers. The problem is, I always get the following error message:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

I tried locating the element by its Xpath, class name and CSS selector but neither of those worked. I found a post that recommended the following method, which also did not work:

driver.find_element_by_partial_link_text("followers").click()

The strangest part is, once the DevTools are open in the same browser, all of the methods I used worked, i.e. opened my list of followers. I am using Microsoft Edge Version 85.0.564.51 btw.

What am I missing here? If needed, I can provide more code.

AMC
  • 2,642
  • 7
  • 13
  • 35
MonteCarlo
  • 26
  • 4

2 Answers2

0
  • 1st method

The followers element in HTML code is an hyperlink tag :

<a class="-nal3 " href="/[PROFILE_ID]/followers/" tabindex="0"><span class="g47SY " title="237">237</span> followers</a>

You can actually go to this URL :

driver.get("https://www.instagram.com/[PROFILE_ID]/followers/")

  • 2nd method

Your method should work with this xpath instead

driver.find_element_by_xpath("//a[contains(.,'followers')]").click()

Henry8
  • 110
  • 12
  • I tried both of these, but neither of them worked for me. The first method simply returns my profile page again and the second method leads to a `TimeoutException`. Using the `CSS_SELECTOR` worked for me however. – MonteCarlo Sep 19 '20 at 08:09
0

On the link of followers is as follows:

<a class="-nal3 " href="/PCy/followers/" tabindex="0">
    <span class="g47SY " title="999">999</span> 
    " followers"
</a>

To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href$='/followers/'] > span"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'followers')]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I actually always use WebDriverWait in combination with `element_to_be_clickable()`. Using the `CSS_SELECTOR` worked for me (thank you), but using the `XPATH` leads to a `TimeoutException`. – MonteCarlo Sep 19 '20 at 08:02
  • Could you please also explain how you derived at the `CSS_SELECTOR` which you provided? I usually copy the selector by right-clicking on the respective element in the DevTools, but by doing this I derive at a different selector. – MonteCarlo Sep 19 '20 at 08:24