0

I am getting stuck at below :

List<WebElement>lstType=driver.findElements(By.xpath("//select[@id='ddlGeneralType']/option"));

Thread.sleep(2000);
int i,j;
System.out.println("the no of type"+lstType.size());

for(i=1;i<=lstType.size();i++){
  lstType.get(i).click();
  List<WebElement> slctsubType=driver.findElements(By.xpath("//select[@id='ddlGeneralSubType']/option")); 

  System.out.println("the no of type"+slctsubType.size());
  for(j=1;j<=slctsubType.size();j++){
    try {
      slctsubType.get(j).click();
      Thread.sleep(5000);
      WebElement save= driver.findElement(By.xpath("//div[@id='generalClassificationEditModal']/div[@class='modal-footer']/a[text()='Save']"));
      save.click();
      Thread.sleep(5000);
      if(save.getText()==null){
        //  driver.findElement(By.xpath("//div[@id='generalClassificationModal']/div[@class='modal-footer']/button[text()='Close']")).click();
          driver.findElement(By.xpath("//a[@href='#classification'and contains(@onclick,'OpenEditGeneralClassification();')]")).click();
      }
      else {
        Thread.sleep(5000);
         //driver.findElement(By.xpath(("//div[@id='generalClassificationEditModal']/div[@class='modal-footer']/button[text()='Close']"))).click();
        driver.findElement(By.xpath(("//div[@id='generalClassificationEditModal']/div[@class='modal-footer']/button[text()='Cancel']"))).click();
        Thread.sleep(5000);
        driver.findElement(By.xpath("//a[@href='#classification'and contains(@onclick,'OpenEditGeneralClassification();')]")).click();
      } 
    }
    catch(org.openqa.selenium.StaleElementReferenceException e){
      e.printStackTrace();
    }
  }
}

I am getting an error message thrown by this line of code:

slctsubType.get(j).click(); 
1
  • Can you give more details of the error? Jul 13, 2015 at 15:50

2 Answers 2

1

There're 2 causes why this happens:

  1. The element has been deleted entirely.
  2. The element is no longer attached to the DOM.

In your case, I think because the element j has not visible in slcsubType so, it cannot click on slctsubType.get(j).click(); I think you should use Explicit Wait and wait until these elements appear,and then click.

Hope this helps.

0

I think you have written the code to select all the the options from drop downlist. but you should select at the most one element. so that you are getting "StaleElementReferenceException in selenium".

It means that some where the reference to the existing element is getting dereferenced, which means element=null.

The problem is here in for loop:

for(j=1;j<=slctsubType.size();j++){
try{
    slctsubType.get(j).click();

In the above code you are selecting all the options from drop down list. you have to select only one option on any condition like value or text.

3
  • Actually there are two drop downs for which I have put two inner loops. It working in such a way that for each drop down ,code should select all the second drop down values one by one and add it on the child modal itself .I put explicit waits also but still its not working out for me. Jun 12, 2015 at 8:27
  • I would like to suggest/recommend you please handle two drop down list individually. don't write nested for loop for two drop down list. Handle it individually. It will help you. if it won't get solve provide more details of your problems. Jun 12, 2015 at 12:10
  • I have two drop downs in such a way that if I select drop down 1 I have to select all the values from drop down2 making drop down 1 as fixed. So both the crop downs are related .I changed code snippet as below but still I get errors: Jun 15, 2015 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.