Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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

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 Selenium-异常而不是布尔值_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java Selenium-异常而不是布尔值

Java Selenium-异常而不是布尔值,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我编写了一个代码,其中我发现结果元素是否可见,但我收到了一个异常 org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@class='airbnb-wide-block-search-btn js-airbnb-search-btn']"} 这是我的密码 wd.navigate().refresh();

我编写了一个代码,其中我发现结果元素是否可见,但我收到了一个异常

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@class='airbnb-wide-block-search-btn js-airbnb-search-btn']"}
这是我的密码

wd.navigate().refresh();
Thread.sleep(7000);
boolean airbnb = wd.findElement(By.xpath(".//*[@class='airbnb-wide-block-search-btn js-airbnb-search-btn']")).isDisplayed();
assertFalse(airbnb, "Airbnb Add will not show After clicking Add one times");

是否有任何建议说明为什么未找到显示的元素?如果未找到元素,则该元素应为
false
我不确定我的错误在哪里?

由于您不希望元素出现,以下代码将引发异常:

boolean airbnb = wd.findElement(By.xpath(".//*[@class='airbnb-wide-block-search-btn js-airbnb-search-btn']")).isDisplayed();
您的
assert
语句甚至无法访问

实际上,您可以在代码中放置预期的异常,而不是断言为(在Junit中):

TestNG
中,语法类似于:

@Test(expectedExceptions = { NoSuchElementException.class })

由于您不希望元素出现,以下代码将引发异常:

boolean airbnb = wd.findElement(By.xpath(".//*[@class='airbnb-wide-block-search-btn js-airbnb-search-btn']")).isDisplayed();
您的
assert
语句甚至无法访问

实际上,您可以在代码中放置预期的异常,而不是断言为(在Junit中):

TestNG
中,语法类似于:

@Test(expectedExceptions = { NoSuchElementException.class })

这就是Selenium的问题所在,您需要使用try-catch-block,如下所示

try
{

        if(wd.findElement(By.xpath(".//*[@class='airbnb-wide-block-search-btn js-airbnb-search-btn']")).isDisplayed())
  {
        //Your code
  }
}
catch (Exception e)
{
        //Your code
}

这就是Selenium的问题所在,您需要使用try-catch-block,如下所示

try
{

        if(wd.findElement(By.xpath(".//*[@class='airbnb-wide-block-search-btn js-airbnb-search-btn']")).isDisplayed())
  {
        //Your code
  }
}
catch (Exception e)
{
        //Your code
}

使用
try-catch
block或使用
throws
Exception捕获
NoSuchElementException

public void methodName() throws Exception
{
    if(wd.findElement(By.xpath(".//*[@class='airbnb-wide-block-search-btn js-airbnb-search-btn']")).isDisplayed())
{
  System.out.println("Element displayed");
} 
}


使用
try-catch
block或使用
throws
Exception捕获
NoSuchElementException

public void methodName() throws Exception
{
    if(wd.findElement(By.xpath(".//*[@class='airbnb-wide-block-search-btn js-airbnb-search-btn']")).isDisplayed())
{
  System.out.println("Element displayed");
} 
}


为了避免异常和昂贵的使用
try-catch
,可以使用
findElements
定位元素。如果结果列表不是空的,您可以检查是否显示了已存在的元素

List<WebElement> elements = wd.findElements(By.xpath(".//*[@class='airbnb-wide-block-search-btn js-airbnb-search-btn']"));
assertFalse(elements.size() > 0 && elements.get(0).isDisplayed(), "Airbnb Add will not show After clicking Add one times");
List elements=wd.findElements(By.xpath(“./*[@class='airbnb-wide-block-search-btn js airbnb-search-btn']);
assertFalse(elements.size()>0&&elements.get(0.isDisplayed(),“单击添加一次后,Airbnb添加将不显示”);

为了避免异常和昂贵的使用
try-catch
,您可以使用
findelelements
定位元素。如果结果列表不是空的,您可以检查是否显示了已存在的元素

List<WebElement> elements = wd.findElements(By.xpath(".//*[@class='airbnb-wide-block-search-btn js-airbnb-search-btn']"));
assertFalse(elements.size() > 0 && elements.get(0).isDisplayed(), "Airbnb Add will not show After clicking Add one times");
List elements=wd.findElements(By.xpath(“./*[@class='airbnb-wide-block-search-btn js airbnb-search-btn']);
assertFalse(elements.size()>0&&elements.get(0.isDisplayed(),“单击添加一次后,Airbnb添加将不显示”);

让我这样说:
IsDisplayed
将在类型为
WebElement
的对象上工作

这是澄清

findElement不应用于查找不存在的元素,而应使用findElements(By)和断言零长度响应


上找到,让我这样说:
IsDisplayed
将在类型为
WebElement
的对象上工作

这是澄清

findElement不应用于查找不存在的元素,而应使用findElements(By)和断言零长度响应


我的问题是,如果iisDisplayed()不返回true,为什么我会使用它?默认情况下,它是displayed()true。这个系统为我工作。但我的问题是,如果iisDisplayed()不返回true,为什么我会使用它?默认情况下,它是displayed()true。我认为在web自动化中,“昂贵”一个<代码>尝试捕获的值可以忽略不计…;)。话虽如此,根据
findElement()
上的Java/Selenium文档,这是我采取的方法,也是一种最佳实践。我认为在web自动化中,
try-catch
的“费用”可以忽略不计……)。话虽如此,这是我采取的方法,根据
findElement()
上的Java/Selenium文档,这是一种最佳实践。