这让我发疯,我尝试过使用By.Id or By.Xpath
但没有任何效果,这是我Select Method
试图从下拉列表中选择文本
//测试代码
SelectMyText(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_ddlCategory']"), "Employee");
//方法:
public void SelectMyText(By locator, string valueToBeSelected)
{
var options = GetElementId(locator).FindElements(By.TagName("option"));
foreach (var option in options)
{
if (valueToBeSelected == option.Text)
{
option.Click();
return;
}
}
}
public IWebElement GetElementId(By locator)
{
return Driver.FindElement(locator);
}
这是得到的错误:
threw exception:
System.NotImplementedException: Element is no longer valid
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.InternalExecute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebElement.get_Text()
以下是此错误的屏幕截图: