I used Java and Selenium Webdriver in a regression testing script. I chose a FirefoxDriver object to perform testing activities. The problem is the following: when the window which is opened by the script is out of focus (for ex., if I manually switched to another program), items of dynamically generated menu are not found. Here is the code:
WebElement locationField = wd.findElement(By.xpath("//input[@id='service_location']"));
locationField.click();
wd.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
List<WebElement> countriesList = wd.findElements(By.xpath("//ul[@id='ui-id-1']/li/a"));
int randomCountryInt = (int) (Math.random() * countriesList.size());
WebElement country = countriesList.get(randomCountryInt); //IndexOutOfBoundsException exception is thrown
country.click();
The menu (“countriesList” in my script) is generated by clicking the “locationField” field.
If I don’t minimize the opened window or don’t switch to another program, the script runs successfully.
Use switch to Window function after clicking location field button.