我对使用C#的硒有点陌生,我有以下代码
GlobalClass glc = new GlobalClass();
glc.waitForElementToBeClickable("select2-mainCorporateCustomers-results",7);
glc.randomClickULOption("select2-mainCorporateCustomers-results");
Thread.Sleep(5000);
glc.ClickDropdownbyText("MyDropDownName");
下面是全局类中的waitForElementToBeClickable方法:
public void waitForElementToBeClickable(string elementID, double waitTime)
{
DefaultWait<IWebDriver> wait = new DefaultWait<IWebDriver>browserDriver);
wait.Timeout = TimeSpan.FromSeconds(waitTime);
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
IWebElement searchResult =
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id(elementID)));
}
以下是我在全局类中的randomClickULOption方法,该方法随机单击给定列表中的项目:
public void randomClickULOption(string ComboID)
{
int itemsCount = 0;
IWebElement comboName = browserDriver.FindElement(By.Id(ComboID));
IList<IWebElement> listItems =
comboName.FindElements(By.TagName("li"));
foreach (IWebElement element in listItems)
{
itemsCount = itemsCount + 1;
}
if (itemsCount==0)
{
Assert.Fail("No items in the combo box");
}
Random rand = new Random();
int iSelect = rand.Next(itemsCount);
//Selecting value from DropDownList
listItems[iSelect].Click();
}
下面是我的ClickDropdownbyText,它通过文本打开了下拉菜单
public void ClickDropdownbyText(string text)
{
var clickPath = "//span[contains(text(), '" + text + "')]";
browserDriver.FindElement(By.XPath(clickPath)).Click();
}
我的问题是,当我运行测试用例时,它将引发以下异常:
OpenQA.Selenium.NoSuchElementException:否这样的元素:无法找到元素:{“ method”:“ xpath”,“ selector”:“ // span [contains(text(),'MyDropDownName')]”“} < / p>
当我在测试用例的以下行上放置断点时:
glc.randomClickULOption("select2-mainCorporateCustomers-results");
并逐步调试代码,以使测试用例顺利通过
请注意,我已尝试增加或减少以下行中的等待时间:
glc.waitForElementToBeClickable("select2-mainCorporateCustomers-results",3);
附加说明:以下代码行实际上并未在网页UI中执行:
browserDriver.FindElement(By.XPath(clickPath)).Click();
我的意思是,在继续执行其余代码之前,该行必须在列表项中单击,这实际上未在网页上发生,但是在调试代码时成功通过了
但是问题仍然存在。那是什么问题呢?