我已经编写了一段代码,该代码在网站上搜索商品,然后选择商品的大小并将其添加到购物袋中。
将商品添加到包中后,我想引入一个while循环,以便它不断增加商品的数量,直到等于或超过£200。我知道while循环将是最好的方法,因为我不知道要执行的循环次数是固定的。
我认为,一旦将一件物品添加到袋子中,就应该引入循环,因为只有在这一点上我才能说出物品的数量。在我的循环中,每增加1时,如何获取代码以验证商品数量的价格。
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 Exercise3
{
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();
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.5 - EU 45.5 - US 11.5");
webDriver.FindElement(By.XPath("//*[@data-bind='text: buttonText']")).Click();
webDriver.FindElement(By.XPath("//a[@data-testid='bagIcon']")).Click();
// I believe the while loop should be implemented here
int number = 200;
while (number > 200)
webDriver.Quit();
}
private static void SelectElementFromDropDown(IWebElement ele, string text)
{
SelectElement select = new SelectElement(ele);
select.SelectByText(text);
}
}
}
答案 0 :(得分:0)
Store the value of the item in a variable before starting the loop and creata a variable with initial value int i =1 and with every time loop runs increment the value by and multiply it by the original value and match is with the value with you fetch from the UI eveytime..
int i = 1
while(condition){
i++
//price for each iteration `enter code here`
i*price for 1 item = "value from the UI"
}
答案 1 :(得分:0)
下面的代码可以添加到您对// I believe the while loop should be implemented here
进行评论的地方
String price = webDriver.FindElement(By.XPath("//span[@class='bag-item-price bag-item-price--current']"))
.getAttribute("innerHTML");
int pricePerUnit = (int) Double.Parse(price.substring(1, price.Length - 1));
int qty = 1;
while (pricePerUnit * qty <= 200) {
qty++;
}
qty--;
IWebElement quantityDropDown = webDriver.FindElement(
By.XPath("//select[@class='bag-item-quantity bag-item-selector select2-hidden-accessible']"));
SelectElementFromDropDown(quantityDropDown, qty.ToString());
答案 2 :(得分:0)
无需使用循环(对于/ 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);
}