Testing Selenium:从下拉列表中选择值,该值取决于在另一个下拉列表中选择的值

Testing Selenium:从下拉列表中选择值,该值取决于在另一个下拉列表中选择的值,testing,selenium,Testing,Selenium,Selenium:我必须从下拉列表中选择值,这取决于在另一个下拉列表中选择的值 我有两个下拉列表1和2。要在2中填充的值取决于1。当我在下拉列表1中选择值时,页面将刷新,并填充2中的值。我必须在下拉列表2中选择值 我收到错误元素不再附加到DOM 我尝试使用wait.until((ExpectedCondition)new ExpectedCondition(),但对我没有帮助。同样的问题也发生了 我试着使用WebElement和Select,但都没用。有人能帮我找出解决方案吗 Javascrip

Selenium:我必须从下拉列表中选择值,这取决于在另一个下拉列表中选择的值

我有两个下拉列表1和2。要在2中填充的值取决于1。当我在下拉列表1中选择值时,页面将刷新,并填充2中的值。我必须在下拉列表2中选择值

我收到错误
元素不再附加到DOM

我尝试使用
wait.until((ExpectedCondition)new ExpectedCondition()
,但对我没有帮助。同样的问题也发生了

我试着使用
WebElement
Select
,但都没用。有人能帮我找出解决方案吗

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);
JavascriptExecutor Executor 2=(JavascriptExecutor)驱动程序;
Execute2.executeScript(“参数[0]。单击();”,);
等待(3000);
Select=newselect(driver.findElement(By.id(“”));
selectCourse.selectByVisibleText(“”);
waitFor(2000年);
Select=new Select(driver.findElement(By.id(“id\u for\u下拉菜单\u 2”));
selectCourse.selectByVisibleText(“”);
waitFor(2000年);

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

元素不再附加…
如果页面被刷新,并且您试图对以前创建的webElement对象执行任何操作,则大多数情况下都会出现。 在这里,页面可能会在选择第一个下拉列表时刷新,看起来您是在第一个下拉列表web元素上执行选择操作,而不是在第二个下拉列表web元素上执行选择操作

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
selectdropdown1=newselect(driver.findElement(By.id(“”));
dropDown1.selectByVisibleText(“”;//应为dropDown1
waitFor(2000年);
//页面可能会在此处刷新
Select dropDown2=new Select(driver.findElement(By.id(“id_代表_drop_down_2”));
dropDown2.selectByVisibleText(“”;//使用dropdwon2而不是dropdown1

有关更多详细信息:

这些是您需要的功能。这将帮助您避免测试过程中由于页面更改而导致测试用例失败。这样的选择标记填充

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");
  }
public void selectByValue(final By,final String value){
act(由,3,新的可调用(){
公共布尔调用(){
Boolean found=Boolean.FALSE;
wait.until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(by));
选择=新选择(driver.findElement(by));
select.selectByValue(值);
found=Boolean.TRUE;//找到它
发现退货;
}
});
}
私有无效行为(By、int-tryLimit、布尔模式、可调用方法){
布尔未找到=真;
int=0;
while(未找到并尝试
您可以添加代码的工作示例吗?编辑了文章并添加了示例代码。是否可以获取页面源代码作为示例,我们无法验证是否存在错误。您是否在最后一块中使用了right select变量?
selectCourse
而不是
selectTopic
?这是不可能的。非常抱歉。是的,我有我们正确的变量。推荐的函数有什么问题?