Новичок в C # здесь. Я написал код, который поступает на веб-сайт, выполняет поиск элемента, нажимает на элемент, добавляет его в сумку, затем нажимает на корзину и продолжает добавлять, пока не будет достигнут предел цены 200, если он достигнут.
Теперь я хочу ввести циклы, чтобы я не жестко кодировал ограничение 200. Вместо этого я хочу, чтобы мой цикл продолжал добавлять элементы до 200. Я видел несколько примеров, подобных приведенному ниже, но я не уверен, как это будет введено в мой существующий код
static void Main()
{
bool 200 = true;
if (200)
{
Console.WriteLine();
}
200 = 200;
if (200)
{
Console.WriteLine(false);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;
namespace Exercise1
{
class Mock1
{
static void Main()
{
IWebDriver webDriver = new ChromeDriver();
webDriver.Navigate().GoToUrl("http://www.asos.com/men/");
webDriver.Manage().Window.Maximize();
webDriver.FindElement(By.XPath(".//input[@data-testid='search-input']")).SendKeys("nike trainers");
webDriver.FindElement(By.XPath(".//button[@data-testid='search-button-inline']")).Click();
// above code goes onto asos.com and searches for nike trainers
WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
IWebElement country = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("article img")));
webDriver.FindElement(By.CssSelector("article img")).Click();
// code to select the size of the trainers and click
IWebElement Size = webDriver.FindElement(By.XPath(".//select[@data-id='sizeSelect']"));
SelectElementFromDropDown(Size, "UK 10 - EU 45 - US 11");
webDriver.FindElement(By.XPath("//*[@data-bind='text: buttonText']")).Click();
wait.Until(ExpectedConditions.ElementExists(By.XPath("//span[text()='Added']")));
webDriver.FindElement(By.XPath("//a[@data-testid='bagIcon']")).Click();
// Adds items to the bag till 200 is reached
wait.Until(ExpectedConditions.ElementExists(By.XPath("//select[contains(@class,'bag-item-quantity')]")));
string totalPrice = webDriver.FindElement(By.XPath("//span[@class='bag-subtotal-price']")).Text;
double pricePerItem = Convert.ToDouble(totalPrice.Substring(1));
int priceLimit = 200;
double noOfQuantity = priceLimit / pricePerItem;
IWebElement qty = webDriver.FindElement(By.XPath("//select[contains(@class,'bag-item-quantity')]"));
SelectElementFromDropDown(qty, Math.Floor(noOfQuantity).ToString());
wait.Until(ExpectedConditions.ElementExists(By.XPath("//button[@class='bag-item-edit-update']")));
webDriver.FindElement(By.XPath("//button[@class='bag-item-edit-update']")).Click();
// webDriver.Quit();
}
private static void SelectElementFromDropDown(IWebElement ele, string text)
{
SelectElement select = new SelectElement(ele);
select.SelectByText(text);
}
}
}