【问题标题】:if element appears, how to close?如果元素出现,如何关闭?
【发布时间】:2019-10-02 03:04:42
【问题描述】:

如果它在运行时出现,我会使用什么命令来关闭这个元素,。我希望我的代码能够在它出现时注意到它,但如果它没有出现,则继续以常规优先级。

【问题讨论】:

  • 可以分享HTML代码吗?

标签: java eclipse selenium automation appium


【解决方案1】:

您可以检查与 id/class 匹配的元素数量,然后仅在至少有 1 个元素时单击。

List<WebElement> elements = driver.findElements(By.id("com.simplemobiletools.gallery:id/parentPanel"));
if (elements.size()>0){
    elements.get(0).click();
}

【讨论】:

    【解决方案2】:
    //You can change ExpectedConditions type (visibilityOfElementLocated)
    public boolean isExist(By elementBy, int seconds) {
        try {
            WebDriverWait wait = new WebDriverWait(driver, seconds);
            wait.until(ExpectedConditions.presenceOfElementLocated(elementBy));
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    //if popup is displayed within 3 seconds
    By POPUP = By.id("com.simplemobiletools.gallery:id/parentPanel");
    
    if (isExist(POPUP, 3)) {
        //do something - for example click cancel button
    }
    

    【讨论】:

    • 我正在使用安卓
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2015-02-06
    相关资源
    最近更新 更多