private void PostNewAdvertisement(MarktplaatsFullAdvert advert) { driver.Navigate().GoToUrl(markpltaatsBasePath); var placeAdButton = driver.FindElementByCSSWithTimeout("a[data-role=\"placeAd\"]"); placeAdButton.Click(); // enter title var titleInput = driver.FindElementByCSSWithTimeout("#category-keywords"); titleInput.TypeSlow(advert.AdvertTitle); // enter category var categorySelect = new SelectElement(driver.FindElementByCSSWithTimeout("#cat_sel_1")); categorySelect.SelectByText(advert.Category); driver.RandomSleep(); // find most suitable subcategory var subCategorySelect = new SelectElement(driver.FindElementByCSSWithTimeout("#cat_sel_2")); var subCatOptions = subCategorySelect.Options; int minDistance = int.MaxValue; string bestCat2Option = null; string bestCat3Option = null; foreach (var subCatOption in subCatOptions) { if (subCatOption.Text.Equals("Kies subgroep") || subCatOption.Text.Equals("Kies categorie")) { continue; } subCategorySelect.SelectByText(subCatOption.Text); var subSubCategorySelect = new SelectElement(driver.FindElementByCSSWithTimeout("#cat_sel_3")); var subSubCatOptions = subSubCategorySelect.Options; foreach (var subSubCatOption in subSubCatOptions) { if (subSubCatOption.Text.Equals("Kies rubriek")) { continue; } var distancdeCat3 = Distance(advert.SubCategory, subSubCatOption.Text); Console.WriteLine($"Ad: {advert.SubCategory} subSubCategory: {subSubCatOption.Text} -- Distance: {distancdeCat3}"); if (minDistance > distancdeCat3) { minDistance = distancdeCat3; bestCat2Option = subCatOption.Text; bestCat3Option = subSubCatOption.Text; } } } subCategorySelect.SelectByText(bestCat2Option); driver.RandomSleep(); new SelectElement(driver.FindElementByCSSWithTimeout("#cat_sel_3")).SelectByText(bestCat3Option); // click next driver.FindElementByCSSWithTimeout("#category-selection-submit").Click(); // fotos string[] images = Directory.GetFiles(tmpImagePath); for (int i = 0; i < advert.NumImages; i++) { var fotoInput = driver.FindElementByCSSWithTimeout($"#uploader-container-{i}").FindElement(By.CssSelector("input[type=\"file\"]")); driver.ScrollToElement(fotoInput); var imageFile = images[i]; string fullPath = Path.Combine(Directory.GetCurrentDirectory(), imageFile); fotoInput.SendKeys(fullPath); driver.RandomSleep(); } // write description driver.RandomSleep(); driver.SwitchTo().Frame("description_nl-NL_ifr"); var description = driver.FindElementByCSSWithTimeout("#tinymce"); driver.ScrollToElement(description); description.TypeSlow(advert.Description); driver.SwitchTo().ParentFrame(); // write price var priceSelector = new SelectElement(driver.FindElementByCSSWithTimeout("#syi-price-type-dropdown > div > select")); priceSelector.SelectByText("Vraagprijs"); driver.RandomSleep(); var price = driver.FindElementByCSSWithTimeout("#syi-bidding-price > input"); driver.ScrollToElement(price); price.TypeSlow(advert.Price.ToString()); // shipping details var shippingSelectorElement = driver.FindElementByCSSWithTimeout("#deliveryMethod > div > select"); var shippingSelector = new SelectElement(shippingSelectorElement); driver.ScrollToElement(shippingSelectorElement); switch (advert.ShippingDetails) { case ShippingDetailType.Delivery: shippingSelector.SelectByText("Verzenden"); break; case ShippingDetailType.Pickup: shippingSelector.SelectByText("Ophalen"); break; case ShippingDetailType.PickupAndDelivery: shippingSelector.SelectByText("Ophalen of Verzenden"); break; } // shipping price var andersButton = driver.FindElementByCSSWithTimeout("#syi-shipping-method > label.form-label.syi-shipping-method-1 > span"); driver.ScrollToElement(andersButton); andersButton.Click(); var shippingPriceInput = driver.FindElementByCSSWithTimeout("#shipping-options > div:nth-child(3) > div > input"); driver.ScrollToElement(shippingPriceInput); shippingPriceInput.Clear(); shippingPriceInput.TypeSlow(advert.ShippingPrice.ToString()); // select free option var freeOption = driver.FindElementByCSSWithTimeout("#js-products").FindElement(By.CssSelector("div[data-val=\"FREE\"]")); driver.ScrollToElement(freeOption); freeOption.Click(); // Post ad var confirmPlaceAdButton = driver.FindElementByCSSWithTimeout("#syi-place-ad-button > span"); driver.ScrollToElement(confirmPlaceAdButton); confirmPlaceAdButton.Click(); }
private MarktplaatsFullAdvert DownloadFullAdvert(string adURL, string title) { // Get attributes driver.Navigate().GoToUrl(adURL); double price = GetPrice(driver.FindElementByCSSWithTimeout("span[data-value=\"price\"]").Text); double shippingPrice = GetPrice(driver.FindElementByCSSWithTimeout(".shipping-details-value.price").Text); string description = driver.FindElementByCSSWithTimeout("#vip-ad-description").Text; ShippingDetailType shippingDetails = GetShippingDetails(driver.FindElementByCSSWithTimeout(".shipping-details-value").Text); IWebElement categoryDescription = driver.FindElementByCSSWithTimeout(".category-description"); string category = categoryDescription.FindElements(By.TagName("p"))[0].Text; string subCategory = categoryDescription.FindElements(By.TagName("p"))[1].Text; int numImages = 0; // click on first image driver.RandomSleep(); driver.FindElement(By.ClassName("carousel-scroll")).FindElements(By.TagName("a"))[0].Click(); driver.RandomSleep(); driver.FindElement(By.Id("vip-image-viewer")).Click(); // Download Images while (true) { var carouselList = driver.FindElementByCSSWithTimeout(".mp-Carousel-list"); string imageSrc = carouselList.FindElements(By.TagName("img"))[numImages].GetAttribute("src"); if (!Directory.Exists(tmpImagePath)) { Directory.CreateDirectory(tmpImagePath); } using (WebClient client = new WebClient()) { client.DownloadFile(new Uri(imageSrc), $"{tmpImagePath}\\image_{numImages}.png"); } // Move to next image numImages++; try { driver.FindElementByCSSWithTimeout("#fullscreen-image-dialog > div.mp-Dialog-content > div:nth-child(2) > div > a.mp-Carousel-nav--right.mp-Button.mp-Button--round > span").Click(); driver.RandomSleep(); } catch (WebDriverTimeoutException) { break; } } // escape out driver.FindElementByCSSWithTimeout(".mp-Dialog-close").Click(); var fullAdvert = new MarktplaatsFullAdvert { AdvertTitle = title, Price = price, ShippingPrice = shippingPrice, Description = description, ShippingDetails = shippingDetails, Category = category, SubCategory = subCategory, NumImages = numImages }; return(fullAdvert); }