如果出现元素,如何关闭?

时间:2019-05-15 03:32:01

标签: java eclipse selenium automation appium

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

enter image description here

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

//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
}