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. 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. 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);
CodePudding user response:
First you need to click on lowest/highest
price link and then use FindElements()
to get list of elements and then iterate
//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
//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]);