繁体   English   中英

我如何获得多个价格数据并使用 Selenium 将它们排序并放入带有 c# 的标签?

[英]How can i get plural price data and put them order and put in labels with c# using Selenium?

我正在尝试做一个价格比较网站。 使用此代码,我可以获得第一个产品价格。 但我需要获取 6 个产品价格并贴上标签,并使用“购买”按钮我想向用户发送购买链接。 另外,我想订购从低到高或从高到低的选项。 我怎样才能做到这一点?

protected void Button0_Click0(object sender, EventArgs e)
{
    IWebDriver driver = new ChromeDriver();
    string str1, str2, str3, str4, str5, str6;
    driver.Url = "https://www.akakce.com/arama/?q=" + TextBox2.Text;
    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
    str1 = driver.FindElement(By.XPath("//span[@class='pb_v8']//span[@class='pt_v8']")).GetAttribute("textContent");
    Label1.Text = ("Price: " + str1);
}

我做了一些更改,但 str 1 给出了第一个正确的数据,但 str 2 给出了 4. 产品价格并跳过 2 和 3. 产品价格。 这是我编辑的代码:

IWebDriver driver = new ChromeDriver();
string str1, str2, str3, str4, str5, str6;
driver.Url = "https://www.akakce.com/arama/?q=" + TextBox2.Text;
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
str1 = driver.FindElement(By.XPath("//li[1]//a//span[@class='pb_v8']//span[@class='pt_v8']")).GetAttribute("textContent");
str2 = driver.FindElement(By.XPath("//li[2]//a//span[@class='pb_v8']//span[@class='pt_v8']")).GetAttribute("textContent");
Label1.Text = ("Price: " + str1);
Label2.Text = ("Price: " + str2);

首先,您需要点击lowest/highest价格链接,然后使用FindElements()获取元素列表,然后迭代

//click on lowest to highest
driver.FindElement(By.XPath("//p[@class='lms_v8']//a[2]")).Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
IList<IWebElement> elements = 
driver.FindElements(By.XPath("//h3/following::span[1]//span[@class='pt_v8']"));
foreach (IWebElement element in elements)
    {
      Console.WriteLine(element.GetAttribute("textContent"));
    }

在字符串列表中添加值,然后将其分配给 index..0-5

//click on lowest to highest
            driver.FindElement(By.XPath("//p[@class='lms_v8']//a[2]")).Click();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
            IList<IWebElement> elements = driver.FindElements(By.XPath("//h3/following::span[1]//span[@class='pt_v8']"));
            List<string> prices = new List<string>();
            foreach (IWebElement element in elements)
            {
                prices.Add(element.GetAttribute("textContent"));
            }
            Console.WriteLine(prices[0]);
            Console.WriteLine(prices[1]);
            Console.WriteLine(prices[2]);
            Console.WriteLine(prices[3]);
            Console.WriteLine(prices[4]); 
            Console.WriteLine(prices[5]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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