c# - 在循环 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);
}
}
}
解决方案
如果我正确理解了您的问题,您想使用循环添加项目,直到达到 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);
}
推荐阅读
- ruby-on-rails - 如何在远程为真时呈现闪烁消息
- python-3.x - 有什么方法可以让 python 中的库工作吗?
- ios - 我们如何知道相册中是否已经存在图像?
- scala - 使用 fs2 将流值列表转换为值流
- spring-boot - Spring + SpringSecurity - 未调用AuthenticationEntryPoint
- java - 使用 Java EE 依赖项在 Spring 中创建名为“entityManagerFactory”的 bean 时出错
- javascript - 引用一个全局变量,而封装的变量可用
- c++ - 在 natvis 中使用 std::type_info 进行强制转换
- kotlin-native - 如何为 Kotlin/Native 启用“宽松”内存模型模式?
- java - 以编程方式将地图片段添加到容器将其添加到底部