Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium 2:从浏览器中导航时,在动态生成的菜单中找不到项目';窗户_Java_Selenium - Fatal编程技术网

Java Selenium 2:从浏览器中导航时,在动态生成的菜单中找不到项目';窗户

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

我在回归测试脚本中使用了Java和SeleniumWebDriver。我选择了一个FirefoxDriver对象来执行测试活动。问题如下:当脚本打开的窗口失焦时(例如,如果我手动切换到另一个程序),动态生成的菜单项找不到。代码如下:

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