Java Selenium 2:从浏览器中导航时,在动态生成的菜单中找不到项目';窗户
我在回归测试脚本中使用了Java和SeleniumWebDriver。我选择了一个FirefoxDriver对象来执行测试活动。问题如下:当脚本打开的窗口失焦时(例如,如果我手动切换到另一个程序),动态生成的菜单项找不到。代码如下:Java Selenium 2:从浏览器中导航时,在动态生成的菜单中找不到项目';窗户,java,selenium,Java,Selenium,我在回归测试脚本中使用了Java和SeleniumWebDriver。我选择了一个FirefoxDriver对象来执行测试活动。问题如下:当脚本打开的窗口失焦时(例如,如果我手动切换到另一个程序),动态生成的菜单项找不到。代码如下: WebElement locationField = wd.findElement(By.xpath("//input[@id='service_location']")); locationField.click(); wd.manage().timeouts()
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();
WebElement locationField=wd.findElement(By.xpath(//input[@id='service\u location']);
locationField.click();
wd.manage().timeouts().implicitlyWait(2,TimeUnit.SECONDS);
List countriesList=wd.findelelements(By.xpath(“/ul[@id='ui-id-1']]/li/a”);
int randomCountryInt=(int)(Math.random()*countriesList.size());
WebElement country=countriesList.get(randomCountryInt)//引发IndexOutOfBoundsException异常
country.click();
菜单(“我的脚本中的countriesList”)是通过单击“locationField”字段生成的。
如果我不最小化打开的窗口或不切换到其他程序,脚本将成功运行。单击“位置字段”按钮后使用“切换到窗口”功能
driver.switchTo().window("WindowName");