代码之家  ›  专栏  ›  技术社区  ›  mahan

Selenium—从angularjs组件中选择输入

  •  1
  • mahan  · 技术社区  · 6 年前

    <md-datepicker ng-model="mc.date.from" required="" md-val="">
      <span class="input-group date" style="width:144px">
        <input size="16" type="text"
               class="form-control"
               autocomplete="off">
        <span class="input-group-btn">
        <button class="btn btn-default" tabindex="-1" >
          <i class="glyphicon glyphicon-calendar"></i>
        </button>
        </span>
      </span>
    </md-datepicker>

    input 类型 text . 我已使用以下代码输入 date . 当我无头运行测试时,大多数情况下它都失败了。

    WebElement fromDate = driver.findElement(
        By.tagName("md-datepicker"))
        .findElement(By.tagName("input"));
    
    if (fromDate.getAttribute("value").length() > 0) {
        fromDate.clear();
    }
    fromDate.sendKeys(startDate);
    

    inputs datepicker 我填写的。但当测试达到 datepciker

    如何解决这个问题?

    在编写上述代码之前,我就使用了这个方法。

    public static void waitUntilVisible(By locator) {
        final long startTime = System.currentTimeMillis();
        final Duration duration = Duration.ofSeconds(2);
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .pollingEvery(duration)
            .ignoring(StaleElementReferenceException.class);
    
        while ((System.currentTimeMillis() - startTime) < 91000) {
            try {
                wait.until(ExpectedConditions.presenceOfElementLocated(locator));
                break;
            } catch (StaleElementReferenceException e) {
                log.info("", e);
            }
        }
    }
    
    4 回复  |  直到 6 年前
        1
  •  2
  •   undetected Selenium    6 年前

    作为 <input> 元素是 Angular WebDriverWait网站 为了想要的 要单击的元素 您可以使用以下任一解决方案:

    • cssSelector :

      WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("md-datepicker[ng-model$='from']>span.input-group.date>input.form-control")));
      elem.click();
      elem.clear();
      elem.sendKeys(startDate);
      
    • xpath :

      WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//md-datepicker[contains(@ng-model,'from')]/span[@class='input-group date']/input[@class='form-control']")));
      elem.click();
      elem.clear();
      elem.sendKeys(startDate);
      

    根据您的问题更新功能 waitUntilVisible() 在我看来,你要实现的地方就像是一个纯粹的开销 FluentWait公司 对于 presenceOfElementLocated() StaleElementReferenceException异常 ,通过定制的 预期条件 作为 elementToBeClickable() .

        2
  •  0
  •   Sers    6 年前

    您可以尝试等待元素的可见性:

    WebElement fromDate =
                    new WebDriverWait(driver, 10)
                    .until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("md-datepicker input")));
    
        3
  •  0
  •   Amit Jain    6 年前

    你可以试着用 cssSelector headless

    WebElement fromDate = driver.findElement(
        By.cssSelector(".input-group.date"))
        .findElement(By.cssSelector(".form-control"));
    if (fromDate.getAttribute("value").length() > 0) {
        fromDate.clear();
    }
    fromDate.sendKeys(startDate);
    
        4
  •  0
  •   mahan    6 年前

    我只能在我抓住的地方用一个试块来解决这个问题 StaleElementReferenceException .

       WebElement input;
        try {
            input = driver.findElement(
                By.tagName("md-datepicker"))
                .findElement(By.tagName("input"));
    
        } catch(StaleElementReferenceException e) {
            input = driver.findElement(By.xpath("//md-datepicker/span/input"));
        }
    
        if (input.getAttribute("value").length() > 0) {
            input.clear();
        }
    

    waitUntilVisible 方法等待输入的存在。


    我在2018年10月29日问了这个问题。当时,我正在使用selenium版本 3.14.0 3.141.0 .