-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClimbingShoesCsvGenerator.cs
81 lines (72 loc) · 3.38 KB
/
ClimbingShoesCsvGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using CsvHelper;
using System.Globalization;
namespace WebCrawler
{
public class ClimbingShoesCsvGenerator
{
public static List<ClimbingShoe> GetClimbingShoesInfo()
{
Console.WriteLine("Paste the location of your Chrome executable below: ");
string chromePath = Console.ReadLine();
var options = new ChromeOptions()
{
BinaryLocation = chromePath,
};
options.AddArguments(new List<string>() { "headless", "disable-gpu" });
var browser = new ChromeDriver(options);
string fullUrl = "https://basecamp-shop.com/en/products/climb/climbing-shoes?pa_manufacturer%5B%5D=638&filtered=1";
browser.Navigate().GoToUrl(fullUrl);
browser.Manage().Window.Maximize();
List<ClimbingShoe> climbingShoes = new List<ClimbingShoe>();
var pages = browser.FindElements(By.XPath("//div[@class='paging']/ul/li"));
int numOfPages = pages.Count();
var wait = new WebDriverWait(browser, TimeSpan.FromSeconds(15));
for (int i = 0; i < numOfPages; i++)
{
var links = wait.Until(browser => browser.FindElements(By.XPath("//div[@class='product-grid-item']")));
foreach (var link in links)
{
var shoe = new ClimbingShoe
{
Brand = link.GetAttribute("data-brand").ToString(),
Model = link.GetAttribute("data-name").ToString(),
Price = link.GetAttribute("data-price").ToString(),
Link = "https://basecamp-shop.com" + link.GetAttribute("data-url").ToString(),
};
climbingShoes.Add(shoe);
}
IWebElement clickableNextBtn = wait.Until(browser => browser.FindElement(By.XPath("//a[contains(text(),'Next')]")));
clickableNextBtn.Click();
}
var lastPageLinks = wait.Until(browser => browser.FindElements(By.XPath("//div[@class='product-grid-item']")));
foreach (var link in lastPageLinks)
{
var shoe = new ClimbingShoe
{
Brand = link.GetAttribute("data-brand").ToString(),
Model = link.GetAttribute("data-name").ToString(),
Price = link.GetAttribute("data-price").ToString(),
Link = "https://basecamp-shop.com" + link.GetAttribute("data-url").ToString(),
};
climbingShoes.Add(shoe);
}
return climbingShoes;
}
public static void GenerateCsv(List<ClimbingShoe> climbingShoes)
{
Console.WriteLine("Paste the path where you wish the report to go to: ");
string outputPath = Console.ReadLine();
string reportPath = outputPath + "\\climbingShoes.csv";
using (var writer = new StreamWriter(reportPath))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(climbingShoes);
writer.Flush();
}
Console.WriteLine("CSV Generated!");
}
}
}