2

This is driving me crazy and I have tried using By.Id or By.Xpath but none works and here is my Select Method trying to select the text from Dropdownlist

//Test code

 SelectMyText(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_ddlCategory']"), "Employee");

//method:

     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);
    }

Here is the error getting:

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()

Here are the screen shots of this error:

enter image description here enter image description here

Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • 3
    It could be because the browser has reorganized the DOM. Try wrapping that code in a try catch and if it fails, calling the code again. If it works the 2nd time then that is probably the case and you will need to build 'stabilization code' into your test that retry a reasonable number of times before finally giving up. – lefthandedgoat Sep 14 '12 at 18:21

2 Answers2

2

Well, your question is from category of the questions how it is possible to select an option from dropdown. I usually use 2 approaches: approach 1(using actions builder, advanced user actions 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()

; Some additional info you can get here

second approach (using js directly):

String cssSelector =..blablabla...
JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+cssSelector+"\');");
        stringBuilder.append("x.click();");
        js.executeScript(stringBuilder.toString());

Hope this works for you)

eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
2

the idea is quite simple. when you suspect the element of being unaccessible, invisible, etc you can use js to cheat it) so as following we've got some methods: -getText -clickontheelement -getelementproperty

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);

Hope this helps you)

eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44