Answered Selenium Instagram Scanner(Not complete)

Mariano07

New member
Joined
May 9, 2020
Messages
3
Programming Experience
Beginner
Hello everyone i been trying to do this as my first c# in a long time.

Im struggling with the element selection as i want the scanner to scroll down every pic on the "news feed" and get name and location from the people i follow.
is there a way to scroll down pic by pic doing that?

I dont know what am i missing.

Let me know if you can help Much appreciate it.
Instagram Scanner With Selenium c#:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
//To hold the list of the elements so we get all the links


namespace SeleniumTest
{
    public class Tests
    {
        string test_url = "https://www.instagram.com";
        IWebElement ScrollElement;
        IWebElement name;
        IWebElement location;

        public IWebDriver driver;

        [SetUp]
        public void Setup()
        {
        
            driver = new ChromeDriver();
            driver.Manage().Window.Maximize();

        }

        [Test]
        public void Test1()
        {
            string LoggedIn = LoginAndRemovePopUp("scannerJackson", "scanner2021");
            Console.WriteLine(LoggedIn);
      
            System.Threading.Thread.Sleep(5000);

            GetNameAndLocation();

        }

        public string LoginAndRemovePopUp(string username, string password)
        {
            driver.Url = test_url;

            System.Threading.Thread.Sleep(2000);
            
            //LOGGING IN
            IWebElement userNameField = driver.FindElement(By.Name("username"));
            IWebElement passWordField = driver.FindElement(By.Name("password"));

            userNameField.SendKeys(username);
            passWordField.SendKeys(password);

            IWebElement loginButton = driver.FindElement(By.XPath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button"));

            loginButton.Click();

            System.Threading.Thread.Sleep(5000);

            //Remove popup window
            IWebElement popUpWindow = driver.FindElement(By.XPath("/html/body/div[4]/div/div/div[3]/button[2]"));
            popUpWindow.Click();

            return "Logged in";
        }

        public void ScrollTo()
        {
            //Scroll to a position on screen
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
            //js.ExecuteScript("window.scrollBy(1,1100)");
            //ScrollTo an element
            ScrollElement = driver.FindElement(By.XPath("/html/body/div[1]/section/main/section/div/div[2]/div/article[1]/header"));
            js.ExecuteScript("arguments[0].scrollIntoView(true);", ScrollElement);
        }

        public void GetNameAndLocation()
        {
            
            IWebElement head = driver.FindElement(By.XPath("/html/body/div[1]/section/main/section/div/div[2]/div/article[1]/header/div[2]"));
                //foreach (IWebElement names in head)
                for(int i = 0; i<10; i++)
                {
                    
                    name = driver.FindElement(By.ClassName("e1e1d"));
                    location = driver.FindElement(By.ClassName("JF9hh"));
                    //head = driver.FindElement(By.XPath("/html/body/div[1]/section/main/section/div/div[2]/div/article[1]/header/div[2]"));
                    Console.WriteLine(name.Text);
                    Console.WriteLine(location.Text);
                    ScrollTo();
                }

        }

        
    }
}
 
So you have code on lines 82-91 to loop and try scrolling to a particular item, but I'm not seeing in your ScrottTo() method how you are actually scrolling to the next item or a particular item by index. Any which way, this looks like a JavaScript problem rather than a C# question -- you just happen to be using C# to do some JavaScript operations.

As an aside, something feels wrong about your test script having to login and dismiss the login screen. It feels more like you are using your test script to scrape a web page rather than to test an application. A test app should have hooks to go straight to a screen to test and bypass the login. That's just my opinion, though, maybe integration testing methodology with Selenium has changed over the last 2 years since I had to do work related to integration testing.
 
Instagram have an API. I suggest using it.

I agree with Skydiver. You should also note, Instagram seem to have developed ways of detecting when their site is being "scraped" for content. And any user account/programs detected for doing so have always been banned. You can research that yourself. If you value your Instagram account or the Instagram accounts of your users of your application, I suggest not scraping them.
 
So you have code on lines 82-91 to loop and try scrolling to a particular item, but I'm not seeing in your ScrottTo() method how you are actually scrolling to the next item or a particular item by index. Any which way, this looks like a JavaScript problem rather than a C# question -- you just happen to be using C# to do some JavaScript operations.

As an aside, something feels wrong about your test script having to login and dismiss the login screen. It feels more like you are using your test script to scrape a web page rather than to test an application. A test app should have hooks to go straight to a screen to test and bypass the login. That's just my opinion, though, maybe integration testing methodology with Selenium has changed over the last 2 years since I had to do work related to integration testing.

Basically im not super familiar with js so im trying to use c# havent been coding for 12 years hah so im extra rusty specially with this stuff

i just dont know if this is possible should i give up or if i really want it should i use js instead?

Whats ur recommendation?

@Sheepings i only use this account for this code so any tips on how to bypass the login?
 

Latest posts

Back
Top Bottom