这让我发疯了,我尝试使用By.Id or By.Xpath
,但没有效果,这是我的Select Method
尝试从Dropdownlist中选择文字
//测试代码
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()
以下是此错误的屏幕截图:
答案 0 :(得分:2)
好吧,您的问题来自问题的类别如何从下拉列表中选择一个选项。我通常使用两种方法: 接近1 (使用操作构建器,高级用户操作api):
WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).click();
sbEle = driver.findElement(By.Id("sbEle")).click();
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.MoveToElement(mnEle).perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).click()
您可以获得一些其他信息here
第二种方法(直接使用js):
String cssSelector =..blablabla...
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssSelector+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
希望这适合你)
答案 1 :(得分:2)
public void clickOnTheElement(String cssLocator){
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssLocator+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
}
public String getTextOfTheElement(String cssLocator)
{ JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\""+cssLocator+"\");");
stringBuilder.append("return x.text().toString();") ;
String res= (String) js.executeScript(stringBuilder.toString());
return res;
}
//getElementProperty e.g. color
public String jsGetColor(String css){
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x=$(\'"+css+"\');");
stringBuilder.append("return x.css('color')");
String res= (String) js.executeScript(stringBuilder.toString());
return res;
}
//SO the only thing remains: you have to find css locator of the element you want to interact //with(get property, click on or get text) and pass it to appropriate function:
String myCssLocatorOfTheElement=..blablabla..;
String color=jsGetColor(myCssLocatorOfTheElement);
String text=getTextOfTheElement(myCssLocatorOfTheElement);
clickOnTheElement(myCssLocatorOfTheElement);
希望这可以帮助你)