没有这样的元素:找不到元素:正在验证web元素是否丢失

no such element: Unable to locate element: validating the web element to be missing

本文关键字:元素 验证 web 是否 找不到      更新时间:2023-10-21

我想验证一个场景,其中web元素不应该在selenium web驱动程序中显示,但会抛出一个错误"没有这样的元素:无法定位元素:"让我知道解决它的方法。

try { 
    if (driver.findElement(By.id(Element_ID)).isSelected() == false) {
        //actions 
    } else { 
        //actions 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

我得到了答案,下面是我使用的示例代码。

     try {
        String AU = driver.findElement(By.id(Element_ID)).getText();
        if( AU != "")
        {
            //Actions
        }
    } catch (Exception NoSuchElementException) 
    {
        //Actions
    }

其中Element_Id是一个变量,包含HTML web元素的th Id。

示例1:

try {
    if (!driver.findElement(By.id(Element_ID)).isSelected()) {
        //Do your stuff
    }
} catch (TimedoutException | NoSuchElementException e) {
    //Do your stuff when element is gone or timed out while waiting
}

示例2:

try {
    if (!driver.findElement(By.id(Element_ID)).getText().equals("some string") {
        //Do your stuff
    }
} catch (TimedoutException | NoSuchElementException e) {
    //Do your stuff when element is gone or timed out while waiting
}

示例3:

try {
    if (!driver.findElement(By.id(Element_ID)).getAttribute("innerHTML").equals("some string") {
        //Do your stuff
    }
} catch (TimedoutException | NoSuchElementException e) {
    //Do your stuff when element is gone or timed out while waiting
}

示例4:

try {
    if (!driver.findElement(By.id(Element_ID)).getAttribute("value").equals("some string") {
        //Do your stuff
    }
} catch (TimedoutException | NoSuchElementException e) {
    //Do your stuff when element is gone or timed out while waiting
}

编辑:对不起,没有注意到你回答了自己的问题。干得好