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.
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();
}
}
}
}