在循环C#中使用XPath

时间:2018-07-09 08:07:29

标签: c# selenium selenium-webdriver

我目前有一段代码,可以将物品添加到限制为200的袋子中。但是,我没有引入这个限制,而是要引入一个循环,该循环将不断添加项目,直到达到200。

我遇到的问题是我不确定如何在循环中合并XPath

   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();



            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();

            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();

//我想在这里引入一个循环,这样当将物品添加到袋子中时,它会一直循环直到达到200。

            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);
        }

    }

}

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,则希望使用循环添加项,直到达到200个限制。在这种情况下,您可以像这样修改代码

假设下面一行(第一行)返回您的总价:

string totalPrice = webDriver.FindElement(By.XPath("//span[@class='bag-subtotal-price']")).Text;
double total = Convert.ToDouble(totalPrice)       

    while(total > 200)
    {
         //code for adding items.
         //update total price
         total = Convert.ToDouble(webDriver.FindElement(By.XPath("//span[@class='bag-subtotal-price']")).Text);          
    }