Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 使用ExpectedConditions类断言元素不存在_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java 使用ExpectedConditions类断言元素不存在

Java 使用ExpectedConditions类断言元素不存在,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我正在尝试使用ExpectedConditions类来执行与Selenium相关的断言,但我很难找到断言给定元素的实际最佳实践。我想用这样的东西 Assert.assertFalse(ExpectedConditions.presenceOfElementLocated(By.xpath(myXpath)).apply(driver1)); 但这当然不会编译,因为presenceOfElementLocated返回一个WebElement而不是布尔值,这让我感到沮丧。你认为实现我的目标的最佳实

我正在尝试使用ExpectedConditions类来执行与Selenium相关的断言,但我很难找到断言给定元素的实际最佳实践。我想用这样的东西

Assert.assertFalse(ExpectedConditions.presenceOfElementLocated(By.xpath(myXpath)).apply(driver1));
但这当然不会编译,因为
presenceOfElementLocated
返回一个WebElement而不是布尔值,这让我感到沮丧。你认为实现我的目标的最佳实践是什么

编辑:已接受的问题答案已转换为Java

public static boolean elementXpathPresent(WebDriver driver,String elementXpath) {
    try {
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(elementXpath)));
    } catch (TimeoutException e) {
        return false;
    }   
    return true;
}

如果没有与选择器匹配的
元素,那么使用
ExpectedConditions
最终将抛出
NoTouchElementException
。在这种情况下,您可能希望将try catch与
ExpectedConditions
一起使用,或者只需使用
findelelements()
size()
查看它是否找到任何元素。看到我的答案了吗

这是你的另一个选择。用C#编写,但很容易转换

/// <summary>
/// 
/// </summary>
/// <returns></returns>
public bool Test()
{
    try
    {
        new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(
            ExpectedConditions.ElementExists(By.XPath("MyXpath")));
    }
    catch (NoSuchElementException)
    {
        return false;
    }

    return true;

}

/// <summary>
/// 
/// </summary>
public void Assertion()
{
    Assert.IsTrue(Test());
}
//
/// 
/// 
/// 
公共布尔测试()
{
尝试
{
新WebDriverWait(驱动程序,时间跨度从秒(10))。直到(
ExpectedConditions.ElementExists(By.XPath(“MyXpath”);
}
捕获(无接触元素异常)
{
返回false;
}
返回true;
}
/// 
/// 
/// 
公共无效断言()
{
Assert.IsTrue(Test());
}

检查元素是否不存在的最简单方法是使用
findElement
函数的复数形式(即
findElements
)并检查返回的元素列表的长度。如果元素不存在,则列表大小为0

Assert.assertTrue(driver.findElements(By.xpath(myXpath)).size() == 0);

使用显式等待测试是否有意义实际上取决于您的应用程序。大多数时候,当我检查是否缺少元素时,我有一个使用显式等待的早期测试。例如,如果要检查对搜索表的更改是否会清空该表,则首先检查该表是否已完成刷新,然后检查该表是否为空。前一个检查需要显式等待,后一个检查不需要。

接受的答案是一种很好的方法(我更喜欢用Selenium进行断言)。但是您也可以使用
ExpectedConditions.not()
Assert.true()
来断言元素不存在:

Assert.assertTrue(
    ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(myXpath))).apply(driver1));

是的,我在过去使用了整个
size()
方法,但我在这里读了很多关于预期条件的文章,我想我应该试一试。但是是的,抛出这样的异常当然会对断言造成不利影响。@user2150250请参阅我的编辑。我想这就是你想要的是的,它似乎确实完成了手头的任务。为了简单起见,您只建议使用
size()
方法,对吗?不太好。大多数现代网站都是用某种
AJAX
构建的。如果您确定元素不应该存在,并且只想测试它,那么使用
size()
如果您想测试您知道(应该)存在的元素的存在,那么最好使用上面的代码。老实说,我使用上面的代码,这是这个场景中最好的测试,我想即使时间不多,你也会接受答案,如果这就是你想要的?