gpt4 book ai didi

c# - “DropDownList”具有无效的 SelectedValue,因为它不存在于项目列表中

转载 作者:行者123 更新时间:2023-11-28 20:55:20 36 4
gpt4 key购买 nike

我目前正在测试网络应用的下拉列表功能。我是测试新手,所以我不太确定如何避免出现此错误。在做了一些研究之后,我知道我必须包含一个 try catch block 才能处理异常。到目前为止,当我运行测试时,我能够测试下拉列表中的第一个选项。最终,考虑到每个选项都将用户定向到不同的页面,我试图测试下拉列表中的每个选项。这是我的代码:

public void GoToProductSelection(string choice)
{
this.GoToFacilitySelection();
IWebElement productsDdl = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementExists(By.Id("ctl00_ContentPlaceHolder1_ddlProducts")));
SelectElement options = new SelectElement(productsDdl);
options.SelectByText(choice);
IWebElement product = options.SelectedOption;
IWebElement contBtn;
IWebElement selCheckBox;

string val = (product.GetAttribute("value"));

IWebElement addToOrderBtn = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_btnAddItem"));
addToOrderBtn.Click();

try
{

}
catch (NoAlertPresentException)
{

}
}

最佳答案

我不确定您的问题是关于 try/catch、一般测试还是实际抛出的错误(无效的选定值)。

您的 try/catch 结构应如下所示。

public void GoToProductSelection(string choice)
{
try
{
this.GoToFacilitySelection();
IWebElement productsDdl = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementExists(By.Id("ctl00_ContentPlaceHolder1_ddlProducts")));
SelectElement options = new SelectElement(productsDdl);
options.SelectByText(choice);
IWebElement product = options.SelectedOption;
IWebElement contBtn;
IWebElement selCheckBox;

string val = (product.GetAttribute("value"));

IWebElement addToOrderBtn = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_btnAddItem"));
addToOrderBtn.Click();
}
catch (NoAlertPresentException)
{
//code to handle NoAlertPresentException
}
}

当在 try block 中抛出异常时,CLR 会查找处理此异常的 catch 语句。如果 try block 中没有任何内容,那么该 block 内将永远不会抛出任何异常,并且永远不会命中 catch。

关于c# - “DropDownList”具有无效的 SelectedValue,因为它不存在于项目列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25245742/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com