马里亚诺07
新成员
- 已加入
- 2020年5月9日
- 留言内容
- 3
- 编程经验
- Beginner
大家好,我很长时间以来一直在尝试将其作为我的第一个C#。
我正在为元素选择而苦苦挣扎,因为我想让扫描仪向下滚动"news feed"并从我追随的人那里获得名字和位置。
有没有办法逐个图片向下滚动呢?
我不知道我在想什么。
让我知道您是否可以帮助很多人。
我正在为元素选择而苦苦挣扎,因为我想让扫描仪向下滚动"news feed"并从我追随的人那里获得名字和位置。
有没有办法逐个图片向下滚动呢?
我不知道我在想什么。
让我知道您是否可以帮助很多人。
具有Selenium C#的Instagram扫描器:
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 = "//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();
}
}
}
}