[英]How can i get plural price data and put them order and put in labels with c# using Selenium?
I'm trying to do a price comparing website.我正在尝试做一个价格比较网站。 With this code, I can get the first product price.使用此代码,我可以获得第一个产品价格。 But I need to take 6 product prices and put their labels and with the "buy" button I wanna send users to buy link.但我需要获取 6 个产品价格并贴上标签,并使用“购买”按钮我想向用户发送购买链接。 Also, I wanna to order options like low to high or high to low.另外,我想订购从低到高或从高到低的选项。 How can I do that?我怎样才能做到这一点?
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);
}
I've did some changes but str 1 is giving correct data first one but str 2 gives 4. product price and skipping 2 and 3. products prices.我做了一些更改,但 str 1 给出了第一个正确的数据,但 str 2 给出了 4. 产品价格并跳过 2 和 3. 产品价格。 Here my edited code:这是我编辑的代码:
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);
First you need to click on lowest/highest
price link and then use FindElements()
to get list of elements and then iterate首先,您需要点击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"));
}
Add value in a string list and then assign it to index..0-5在字符串列表中添加值,然后将其分配给 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.