Вы можете реализовать свой сценарий, не используя циклы (цикл for / while). Я добавил логику ниже. Рендеринг некоторых элементов занимает больше времени. Итак, я бы предложил добавить явное условие, где это необходимо.
Пожалуйста, найдите обновленный код и отошлите все комментарии для более подробной информации
Основной метод:
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();
//Search Result rendering will take some times.So, Explicit wait is mandatory
WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
IWebElement country = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("article img")));
webDriver.FindElement(By.CssSelector("article img")).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();
//Add to cart takes some time.So, the below condition is needed
wait.Until(ExpectedConditions.ElementExists(By.XPath("//span[text()='Added']")));
webDriver.FindElement(By.XPath("//a[@data-testid='bagIcon']")).Click();
//Wait condition is needed after the page load
//wait.Until(ExpectedConditions.TitleContains("Shopping"));
wait.Until(ExpectedConditions.ElementExists(By.XPath("//select[contains(@class,'bag-item-quantity')]")));
//Extract the price from the cart
string totalPrice =webDriver.FindElement(By.XPath("//span[@class='bag-subtotal-price']")).Text;
//Extract the price amount by exluding the currency
double pricePerItem = Convert.ToDouble(totalPrice.Substring(1));
// Just hardcoded the expected price limit value
int priceLimit = 200;
double noOfQuantity = priceLimit / pricePerItem;
IWebElement qty =webDriver.FindElement(By.XPath("//select[contains(@class,'bag-item-quantity')]"));
//Quantity values are rounded off with nearest lowest value . Example, 5.55 will be considered as 5 quantity
SelectElementFromDropDown(qty, Math.Floor(noOfQuantity).ToString());
//After updating the quantity, update button will be displayed dynamically.So, wait is added and then update action is performed
wait.Until(ExpectedConditions.ElementExists(By.XPath("//button[@class='bag-item-edit-update']")));
webDriver.FindElement(By.XPath("//button[@class='bag-item-edit-update']")).Click();
Альтернативный подход с использованием цикла:
Я бы настоятельно рекомендовал использовать вышеуказанный подход. так как количество будет увеличиваться по одному за каждый раз, пока ценовой лимит не превысит 200
//Extract the price from the cart
string totalPrice = webDriver.FindElement(By.XPath("//span[@class='bag-subtotal-price']")).Text;
double currentTotalPrice = Convert.ToDouble(totalPrice.Substring(1));
double itemPerPrice = currentTotalPrice;
// Just hardcoded the expected price limit value
int priceLimit = 200;
int quantity = 1;
while(currentTotalPrice < priceLimit && priceLimit-currentTotalPrice > itemPerPrice)
{
wait.Until(ExpectedConditions.ElementExists(By.XPath("//select[contains(@class,'bag-item-quantity')]")));
IWebElement qty = webDriver.FindElement(By.XPath("//select[contains(@class,'bag-item-quantity')]"));
//Quantity values are rounded off with nearest lowest value . Example, 5.55 will be considered as 5 quantity
SelectElementFromDropDown(qty, (++quantity).ToString());
//After updating the quantity, update button will be displayed dynamically.So, wait is added and then update action is performed
wait.Until(ExpectedConditions.ElementExists(By.XPath("//button[@class='bag-item-edit-update']")));
webDriver.FindElement(By.XPath("//button[@class='bag-item-edit-update']")).Click();
wait.Until(ExpectedConditions.ElementExists(By.XPath("//span[@class='bag-subtotal-price']")));
var temp = webDriver.FindElement(By.XPath("//span[@class='bag-subtotal-price']")).Text;
currentTotalPrice = Convert.ToDouble(temp.Substring(1));
Console.WriteLine("Current Price :" + currentTotalPrice);
}