Selenium:从下拉列表中选择值,该值取决于在另一个下拉列表中选择的值 [英] Selenium: Select value from a drop down which is dependent on value selected in another drop down

查看:27
本文介绍了Selenium:从下拉列表中选择值,该值取决于在另一个下拉列表中选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Selenium: I have to Select value from a drop down which is dependent on value selected in another drop down.

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

Ex: I have two drop downs 1 and 2. Value to be populated in 2 is dependent on 1. When I select value in dropdown 1 then page gets refreshed and value in 2 is populated. I have to select value in drop down 2.

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

我尝试使用 wait.until((ExpectedCondition<Boolean>) new ExpectedCondition<Boolean>() 但它对我没有帮助.发生了同样的问题.

I tried using wait.until((ExpectedCondition<Boolean>) new ExpectedCondition<Boolean>() but it does not help me. Same issue occurs.

我尝试使用 WebElementSelect 但都没有帮助.谁能帮我找出解决方案?

I tried using WebElement and Select but neither helped. Can anyone help me figure out solution?

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) 一个定义的函数来等待指定的时间段.

I am using waitFor(2000) a defined function for waiting for specified timeperiod.

推荐答案

这些是您需要的功能.这将对您有所帮助,因此测试用例不会因测试期间页面更改而失败.这样一个选择标签的人口.

These are the functions you need. This will help you so the test cases will not fail due to page changing during testing. Such a population of a select tag.

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

这篇关于Selenium:从下拉列表中选择值,该值取决于在另一个下拉列表中选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆