I got stuck in for loop as the automation code is entering a loop that has few actions to be done like this:
for (i = 0; i <= 2; i++) {
// if(i==2)break;
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebDriverWait waitForInvstrDmo = new WebDriverWait(driver, 300);
waitForInvstrDmo.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@href='#transferorDemographicsModal' and contains(@onclick,'transferorEditDemographics();')]")));
driver.findElement(By.xpath("//a[@href='#transferorDemographicsModal' and contains(@onclick,'transferorEditDemographics();')]")).click();
WebDriverWait waitForCitizn = new WebDriverWait(driver, 50);
waitForCitizn.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='ddlTransferorCitizenship']")));
Select selectCitiZenship = new Select(driver.findElement(By.xpath("//select[@id='ddlTransferorCitizenship']")));
selectCitiZenship.selectByValue("235");
WebDriverWait waitForTaxResidency = new WebDriverWait(driver, 80);
waitForTaxResidency.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='ddlTransferorTaxResidency']")));
Select selectTaxResidency = new Select(driver.findElement(By.xpath("//select[@id='ddlTransferorTaxResidency']")));
selectTaxResidency.selectByValue(String.valueOf((int) (Math.random() * 50)));
WebDriverWait waitForBirthPlace = new WebDriverWait(driver, 80);
waitForBirthPlace.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='ddlTransferorBirthPlace']")));
Select selectBirthPlace = new Select(driver.findElement(By.xpath("//select[@id='ddlTransferorBirthPlace']")));
selectBirthPlace.selectByValue(String.valueOf((int) (Math.random() * 50)));
WebDriverWait waitForCountryOfIncor = new WebDriverWait(driver, 100);
waitForCountryOfIncor.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='ddlTransferorIncorporationCountry']")));
Select selectCountryOfIncor = new Select(driver.findElement(By.xpath("//select[@id='ddlTransferorIncorporationCountry']")));
selectCountryOfIncor.selectByValue(String.valueOf((int) (Math.random() * 50)));
WebDriverWait waitForDomCountry = new WebDriverWait(driver, 300);
waitForDomCountry.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='ddlTransferordomicileCountry']")));
Select selectDomCountry = new Select(driver.findElement(By.xpath("//select[@id='ddlTransferordomicileCountry']")));
selectDomCountry.selectByValue(String.valueOf((int) (Math.random() * 50)));
WebDriverWait waitForSaveDemog = new WebDriverWait(driver, 300);
waitForSaveDemog.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='modal-footer']/a[@href='#'and contains(@onclick,'SaveTransferorDemographics();')]")));
// code for saving the Investor Demographic details
driver.findElement(By.xpath("//div[@class='modal-footer']/a[@href='#'and contains(@onclick,'SaveTransferorDemographics();')]")).click();
}
Now point is when I am executing this code for the first iteration its taking the select values from the drop down as mentioned in the code but its getting stuck in final iteration for 'selectCitiZenship' & 'selectTaxResidency' in above snippet within the loop. So in this case whole code is executing well for 1 and 2 iteration but it will failed in the last iteration throwing a TimeOut Exception - "Timed out after 50 seconds waiting for visibility of element located by By.xpath: //select[@id='ddlTransferorCitizenship']". I changed the wait timings too. Please provide me your valuable suggestions if you have.
P.S. Its working well for the first iteration but not for the rest iteration.