0

I'm trying to make a simple C# program which will give me a list of followers of my public profile. I managed to get the driver connect to the provided URL and now I'm trying to click on the Followers button but get the exception:

OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"css selector","selector":".\-nal3"}

I read in another SO post about having to wait for the page to load before trying to click the element but the solution doesn't work for me (org.openqa.selenium.ElementClickInterceptedException: element click intercepted error using Selenium and Java in headless mode).

My code so far:

public void Run(string profileName)
{
    ChromeOptions options = new ChromeOptions();
    options.AddArgument("--headless");
    options.AddArgument("--no-sandbox");
    options.AddArgument("--disable-dev-shm-usage");
    options.AddArgument("start-maximized");
    options.AddArgument("--remote-debugging-port=9222");
    IWebDriver driver = new ChromeDriver(options);

    string url = "http://www.instagram.com/" + profileName + "/";

    driver.Navigate().GoToUrl(url);

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    IWebElement followersButton = wait.Until(driver => driver.FindElement(By.ClassName("-nal3")));

    followersButton.Click();
}

I've also tried finding the element by the XPath with the same result.

What am I doing wrong?

UPDATE: I've created a separate string variable in my code to easily debug and see the source of the page which is returned and then opened Visual Studio's HTML Visualizer and it shows a page which says some active elements were restricted. How can I enable them in my code? enter image description here

Val
  • 1,548
  • 1
  • 20
  • 36
  • Doesn't IG have an API you can use to get this information? – Neil Apr 27 '21 at 21:18
  • From what I read last time (a few months ago) they closed the API for the public and only verified and approved applications access it. – Val Apr 27 '21 at 21:22
  • The legacy API has been closed, but the new API still works. Sure you have to create a FB developer account, and jump through a few hoops, but they aren't 'blocking' you from doing that. – Neil Apr 28 '21 at 08:14
  • @Neil, can you link to some tutorials for that. I see thousands of different results in Google but they seem to be mostly some 3rd-party solutions. – Val Apr 28 '21 at 11:17
  • I'd start by reading through the official documentation https://developers.facebook.com/docs/instagram-basic-display-api/getting-started – Neil Apr 28 '21 at 15:06

1 Answers1

0

The problem is there are 3 of those -nal3 Class Names on that page, one for posts, followers, and following

Try this XPath instead, to narrow it down to just the followers link:

//ul[@class='k9GMp ']/li[2]/a
JD2775
  • 3,658
  • 7
  • 30
  • 52
  • Nope. Throws exception `OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"xpath","selector":"//ul[@class='k9GMp ']/li[2]/a"}` – Val Apr 27 '21 at 21:37
  • At code line: `IWebElement followersButton = wait.Until(driver => driver.FindElement(By.XPath("//ul[@class='k9GMp ']/li[2]/a")));` – Val Apr 27 '21 at 21:38
  • What page are you on specifically when you are trying to find that Element? Are you logged in and on your profile page? I don't see any logIn code in your original post so I don't know. – JD2775 Apr 27 '21 at 21:40
  • I am logged in when I copy the class name in my browser. But I guess the Chrome instance Selenium is running might have a different class name for that same button. Let me check that. – Val Apr 27 '21 at 21:45
  • I see Instagram does show a completely different page if you're not logged in - the login page. But I've just tried locating and clicking a link on that login page (the About link) and I still have the same problem. I copied the XPath to the link into my code and still get the "element not found" exception. – Val Apr 27 '21 at 21:51
  • Now I'm also explicitly telling the driver to open the login page (instead expecting Instagram to redirect my program there) and it still can't find the About link on that page. – Val Apr 27 '21 at 21:53
  • I get a different style of LogIn page everytime I try to access my account from a fresh Selenium/Chrome browser. I don't have time to mess around with the LogIn piece of the code, sorry. I do know though that your original ClassName element `-nal3` (once you actually are on the Profile Page) is incorrect, as there are 3 of them. I validated in Chrome Dev Tools that the XPath I provided you is a single element and should work correctly, maybe try adding some manual sleeps just for debugging purposes – JD2775 Apr 27 '21 at 22:01
  • In what sense is the page's style different for you? – Val Apr 27 '21 at 22:04
  • Open up Chrome to a specific profile page in IG (manually, not in Selenium), click `F12`, put the dockside at the bottom of the screen, in the HTML section hit `ctrl-f`, that pops up a Find window at the bottom. In the Find window type `-nal3`. You will see on the right of the Find window the number 3. You can arrow up and down to see the 3 `nal3` elements on the page.. Whereas if you put the `xpath` I noted in my reply you will see only 1 element, matching to the `followers` link you want – JD2775 Apr 27 '21 at 22:06