我有两个不同的下拉列表,第一个下拉列表包含5个值,第二个下拉列表包含十个值,我的问题是我想迭代我的下拉列表中的每个值,在第一个下拉所有选项中应该验证,我已经编写了如下代码,但经过两次迭代后,它给出了#34; staleElementReferenceException" ..请帮助我。
public static void data() throws Exception
{
dept = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='dept']"));
Select deptdropbox = new Select(dept);
listb= deptdropbox.getOptions();
for ( int s=0; s<listb.size(); s++)
{
System.out.println(listb.get(s).getText());
listb.get(s).click();
Thread.sleep(2000);
region = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='SVC_UNIT_NAME']"));
Select regiondropbox = new Select(region);
regionlist = regiondropbox.getOptions();
for( int d=0; d<regionlist.size();d++)
{
System.out.println(regionlist.get(d).getText());
String DD2 = regionlist.get(d).getText();
regionlist.get(d).click();
}
}
}
答案 0 :(得分:0)
以下代码可能对您有帮助,
public static void data() throws Exception
{
dept = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='dept']"));
Select deptdropbox = new Select(dept);
listb= deptdropbox.getOptions();
for ( int s=0; s<listb.size(); s++)
{
deptdropbox.selectByIndex(s);
System.out.println(deptdropbox.getFirstSelectedOption().getText());
Thread.sleep(2000);
region = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='SVC_UNIT_NAME']"));
Select regiondropbox = new Select(region);
regionlist = regiondropbox.getOptions();
for( int d=0; d<regionlist.size();d++)
{
regionlist.selectByIndex(d);
System.out.println(regionlist.getFirstSelectedOption().getText());
}
}
}
答案 1 :(得分:0)
你应该花点时间阅读StaleElementException
是什么,为什么会引起它,以及如何解决它。你可能会再遇到这样的情况。
问题(显然)是第一个SELECT
在某些时候发生了变化,这意味着你指向它的任何变量都会在你尝试引用它时引发异常。在您的情况下,您需要做的是在返回到循环顶部之前获取对第一个下拉列表的新引用。我试图简化一些事情。
public static void data() throws Exception
{
By deptLocator = By.id("dept");
By regionLocator = By.id("SVC_UNIT_NAME");
WebElement dept = TestNGUtils.getWebDriver().findElement(deptLocator);
List<WebElement> listb = new Select(dept).getOptions();
for (int s = 1; s < listb.size(); s++)
{
// grab a reference to dropdown 2 so we can wait for it to go stale later
WebElement region = TestNGUtils.getWebDriver().findElement(regionLocator);
System.out.println(listb.get(s).getText());
listb.get(s).click();
// wait for the reference to dropdown 2 to go stale as the result of making a selection in dropdown 1
new WebDriverWait(driver, 10).until(ExpectedConditions.stalenessOf(region));
region = TestNGUtils.getWebDriver().findElement(regionLocator); // now that it's stale, get it again
List<WebElement> regionlist = new Select(region).getOptions();
for (int d = 0; d < regionlist.size(); d++)
{
System.out.println(regionlist.get(d).getText());
regionlist.get(d).click();
}
// get a reference to dropdown 1 again since it's now stale
dept = TestNGUtils.getWebDriver().findElement(deptLocator);
listb = new Select(dept).getOptions();
}
}
答案 2 :(得分:0)
@ tadimarriakhilabegum-tadimarri下面的代码应该可行......但它并没有优化的方式。
public static void data() throws Exception
{
dept = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='dept']"));
Select deptdropbox = new Select(dept);
listb= deptdropbox.getOptions();
for ( int s=0; s<listb.size(); s++){
Select deptdropbox1 = new Select(dept);
listc= deptdropbox1.getOptions();
System.out.println(listc.get(s).getText());
listc.get(s).click();
Thread.sleep(2000);
region = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='SVC_UNIT_NAME']"));
Select regiondropbox = new Select(region);
regionlist = regiondropbox.getOptions();
for( int d=0; d<regionlist.size();d++){
System.out.println(regionlist.get(d).getText());
String DD2 = regionlist.get(d).getText();
regionlist.get(d).click();
}
}
}