首页 > 解决方案 > 在循环 C# 中使用 XPath

问题描述

我目前有一段代码可以将物品添加到限制为 200 的袋子中。但是,我不想设置此限制,而是想引入一个循环,该循环将继续添加项目,直到达到 200。

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

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

    }

}

标签: c#seleniumselenium-webdriver

解决方案


如果我正确理解了您的问题,您想使用循环添加项目,直到达到 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);          
    }

推荐阅读