Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium-从angularjs组件中选择输入_Java_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Java Selenium-从angularjs组件中选择输入

Java Selenium-从angularjs组件中选择输入,java,selenium,xpath,css-selectors,webdriverwait,Java,Selenium,Xpath,Css Selectors,Webdriverwait,由于元素是一个元素,因此您必须引导WebDriverWait使所需元素可单击,并且您可以使用以下任一解决方案: css选择器: WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("md-datepicker[ng-model$='from']>span.input-group.date>input.form-cont


由于
元素是一个元素,因此您必须引导WebDriverWait使所需元素可单击,并且您可以使用以下任一解决方案:

  • css选择器

    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,可以通过定制的WebDriverWait轻松实现,预期条件为
元素ToBelickable()
您可以尝试等待元素的可见性:

WebElement fromDate =
                new WebDriverWait(driver, 10)
                .until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("md-datepicker input")));

您可以尝试使用
cssSelector
,不建议在执行
headless
时使用xpath

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

我只能用try-a-catch块来解决这个问题,在该块中我捕获
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
。但是这种方法选择输入,您也不应该使用selenium版本
3.141.0

谢谢你的回答。如果省略
span.input group.date
,该怎么办?当我有一个angularjs元素时,我应该等待吗?WebDriverWait对angularjs元素有什么影响?我相信它只会在同步中起作用,直到元素可以点击为止。。。。我们需要做什么特殊的事情来处理角度元素吗?@mahan-angular处理承诺,如果承诺没有解析,元素可能无法加载,因此需要对角度元素进行良好的同步,就像我们处理ajax元素一样。但是我不确定如果你的应用程序中有更多的角度元素,那么webdriver的等待会对角度元素有多大帮助recommended@mahan正如你提到的…在我填写的日期选择器之前还有一些其他输入。。。因此,为了实现精确匹配,在构造节点时考虑了祖先节点,即
,子代节点,即
,以及下一个子代节点,即
。但是,如果省略
span.input group.date
仍然唯一地标识所需的元素,这将是最理想的方法。@AmitJain WebDriverWait是处理元素的唯一方法,它有助于同步您是否等待元素的存在/可见性/可交互性。承诺是通过Selenium的Java客户机在内部处理的,OP正在使用该客户机,因此考虑Promise和ng webdriver不属于这个问题的范围。因此,我使用的是
md datepicker
我使用过很多datepicker,你会发现不同输入或日期文本的css分类不同,所以你可以区分它们。我相信selenium不适用于任何ng元素,它不会稳定地识别它们。我同意你的看法。但输入的父项没有唯一标识符。