2015-05-12 28 views
1

Selenium:我必须从下拉菜单中选择值,这取决于在另一个下拉菜单中选择的值。Selenium:从下拉菜单中选择一个值,该值取决于在另一个下拉菜单中选择的值。

例如:我有两个下拉菜单1和2.在2中填充的值取决于1.当我在下拉菜单1中选择值时,页面被刷新并且填充值2。我必须在下拉列表中选择数值2.

我收到错误Element is no longer attached to DOM

我试过使用wait.until((ExpectedCondition<Boolean>) new ExpectedCondition<Boolean>()但它不帮助我。同样的问题发生。

我尝试使用WebElementSelect,但都没有帮助。任何人都可以帮我找出解决方案吗?

JavascriptExecutor executor2 = (JavascriptExecutor)driver; 
executor2.executeScript("arguments[0].click();", <elementname>); 
waitFor(3000); 

Select <objectname1>= new Select(driver.findElement(By.id("<ID_for_drop_down_1>"))); 
selectCourse.selectByVisibleText("<valuetobeselected>"); 
waitFor(2000); 

Select <objectname2>= new Select(driver.findElement(By.id("ID_for_drop_down_2"))); 
selectCourse.selectByVisibleText("<valuetobeselected>"); 
waitFor(2000); 

我正在使用waitFor(2000)定义的函数来等待指定的时间段。

+0

您可以添加代码的工作示例吗? – MeanGreen

+0

编辑帖子并添加示例代码。 – Abhinav

+0

是否有可能以页面源为例,我们不能验证是否有错误。 – Dude

回答

1

这些都是你需要的功能。这将对您有所帮助,这样测试用例不会因测试过程中的页面更改而失败。选择标签的这种人口。

public void selectByValue(final By by, final String value){ 
    act(by, 3, new Callable<Boolean>() { 
     public Boolean call() { 
     Boolean found = Boolean.FALSE; 

     wait.until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(by))); 

     Select select = new Select(driver.findElement(by)); 

     select.selectByValue(value); 
     found = Boolean.TRUE; // FOUND IT 

     return found; 
     } 
    }); 
    } 

private void act(By by, int tryLimit, boolean mode, Callable<Boolean> method){ 

    boolean unfound = true; 
    int tries = 0; 
    while (unfound && tries < tryLimit) { 
     tries += 1; 
     try { 
     wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOfElementLocated(by))); 
     unfound = !method.call(); // FOUND IT, this is negated since it feel more intuitive if the call method returns true for success 
     } catch (StaleElementReferenceException ser) { 
     logger.error("ERROR: Stale element exception. "); 
     unfound = true; 
     } catch (NoSuchElementException nse) { 
     logger.error("ERROR: No such element exception. \nError: "+nse); 
     unfound = true; 
     } catch (Exception e) { 
     logger.error(e.getMessage()); 
     } 
    } 

    if(unfound) 
     Assert.assertTrue(false,"Failed to locate element"); 
    } 
+0

推荐的功能有什么问题? –

1

Element no longer attached...如果刷新页面并尝试对先前创建的webElement对象执行任何操作,通常会出现该页面。 此页面可能会在选择第一个下拉列表时刷新,看起来您正在对第一个下拉网页元素执行选择操作,而不是第二个。

Select dropDown1= new Select(driver.findElement(By.id("<ID_for_drop_down_1>"))); 
dropDown1.selectByVisibleText("<valuetobeselected>"); // Should be dropdown1 
waitFor(2000); 
// Page might be refreshed here 
Select dropDown2= new Select(driver.findElement(By.id("ID_for_drop_down_2"))); 
dropDown2.selectByVisibleText("<valuetobeselected>"); // use dropdwon2 not dropdown1 

有关详细信息:Random "Element is no longer attached to the DOM" StaleElementReferenceException

相关问题