1

I am using Selenium to find elements in an Instagram page.

I have successfully logged in and loaded a users profile, however am unable to load their following box.

When in a browser if you click the following link it loads up a box that you can scroll to see the following list.

When using selenium I have done the following -

var opt = new PhantomJSOptions();
        opt.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");

var driver = new PhantomJSDriver(driverService, opt);

var element = driver.FindElementByName("username");
    element.SendKeys(userName);
    element = driver.FindElementByName("password");
    element.SendKeys(passWord);

    // find the login button and click it after credentials have been populated
    element = driver.FindElement(By.XPath("//button[.='Log in']"));
    element.Click();

    // Now navigate to the users page
    driver.Navigate().GoToUrl("https://www.instagram.com/" + accName);

At this stage I am successfully logged in, on the users page and can find the following link and click it by doing -

var element = driver.FindElementByPartialLinkText("following");
element.Click();

However on the click event, this bizarrely takes me back to the main login page for Instagram.

Another thing I noticed is that if you are in the browser and click the link, the url in the browser updates to -

https://www.instagram.com/{username}/following/

However if you copy this link and paste it into a new window and try navigating to it it does not load the following box automatically as I expected, and just takes you to the users main page.

I have found examples in python where people seem to be successful using the click event e.g. example of using python.

How can I achieve this using the C#? I am not sure if Instagram have blocked this action completely.

I'm not sure if it is important but I am using a slightly older version of the Selenium Web Driver 3.8.0. It had also occurred to me to perhaps use a ChromeDriver instead of PhantomJS however not sure if this would make any difference.

Ebikeneser
  • 2,582
  • 13
  • 57
  • 111

2 Answers2

1

Another thing I noticed is that if you are in the browser and click the link, the url in the browser updates to -

https://www.instagram.com/{username}/following/

However if you copy this link and paste it into a new window and try navigating to it it does not load the following box automatically as I expected, and just takes you to the users main page.

If I'm not mistaken this happens because on clicking this button the POST request is sent: enter image description here

But when you past this URL in the browser then the GET request is made and it lacks some needed data.

I'd suggest you to debug your issue to check the next things:

What happens when you make a brakepoint before clicking the link? If it works well then you need to wait for page/scripts loading. There are a lot of nuances in how to do it. You can look here for help: Wait for page load in Selenium.

The link to Python code you gave have simply code that is not hard to convert to C# but the author doesn't describe your issue there. Your issue now is that you cannot invoke needed popup so work on it.

Thread.Sleep() is not preferred method to use but you can use it to debug if it helps. If it helps and you don't wanna dig deep into how to wait page/scripts loading you can leave it as it is.

Denis Koreyba
  • 3,144
  • 1
  • 31
  • 49
-3

Unfortunately I don't have an Instagram account to be able to try this out for you fully, but given the behaviour is as though you are not logged in my assumption would be that you are not waiting long enough for the server to come back with the authentication success between clicking the "Log in" button and navigating to the user's page.

I would recommend adding a Thread.Sleep(5000) after clicking the "Log in" button as an initial investigation to wait 5 seconds for the login process to complete and see if that works - if it does, I would advise you then replace this thread sleep to instead wait for an element which exists on the page which you are redirected to following successful login. That way, you are always waiting for the login process to complete before navigating to the user's page and the behaviour should then hopefully be as expected

KintoborML
  • 65
  • 5