简体   繁体   中英

extracting same result using selenium c#

I'm extracting posts from instagram using selenium, so I get image src and its text. the problem is that while extracting it's scrape same src and different text for such post.

Here is my code

var options = new ChromeOptions();
options.AddArguments("--disable-gpu");
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var dr = new ChromeDriver(driverService, options);
for (int d = 0; d < listBox1.Items.Count; d++)
{
    dr.Navigate().GoToUrl("https://www.instagram.com/" + listBox1.Items[d].ToString());
    var number = dr.FindElementByClassName("_fd86t").Text;
    Thread.Sleep(100);
    dr.FindElementByClassName("_si7dy").Click();
    for (int s = 0; s < Convert.ToInt32(numericUpDown1.Value); s++)
    {
        Thread.Sleep(1000);

        if (dr.FindElements(By.XPath("/html/body/div[4]/div/div[2]/div/article/div[1]/div/div/div/div[2]/a")).Count != 0)
        {
            dr.Keyboard.SendKeys(OpenQA.Selenium.Keys.Right);
        }
        else
        {
            var src = dr.FindElementByClassName("_2di5p").GetAttribute("src");
            var pra = dr.FindElementByClassName("_ezgzd").FindElement(By.TagName("span")).Text;
            this.Invoke(new MethodInvoker(delegate ()
            {
                dataGridView1.Rows.Add(src.ToString(), pra.ToString());

            }));
            dr.Keyboard.SendKeys(OpenQA.Selenium.Keys.Right);

        }
    }
}

Try this:

Replace the class name Find By to xpath, By adding "." it will pick the relevant node. else it picks the first occurence of this class.

dr.FindElementByXpath(".//*[@class='_si7dy'").GetAttribute("src");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM