如何在找不到元素时保持代码运行

时间:2021-06-05 15:32:38

hi there my code keep running an error when this element is not found

您好,我的代码在找不到此元素时仍会运行错误

driver.findElement(By.xpath("(//span[@class='_soakw coreSpriteLikeHeartOpen'])")).click();

can anyone help me? i want the other code to keep running even though this element is not found I've been looking for the answer the whole day on the internet

谁能帮我?我希望其他代码继续运行,即使找不到这个元素我一直在互联网上寻找答案

2 个解决方案

#1


1  

You can place a try catch block around the find element. After the catch blok the execution of the code wil continue. The function findelement throws a NoSuchElementException when there is no element found.

您可以在find元素周围放置一个try catch块。在捕获blok之后,代码的执行将继续。当没有找到元素时,函数findelement抛出NoSuchElementException。

// Set the timeout for searching an element
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try 
{
    // Try to find the element
    driver.findElement(By.xpath("(//span[@class='_soakw coreSpriteLikeHeartOpen'])")).click();
} 
catch (NoSuchElementException e)
{
    System.out.println("Element Not Found");
}
// Continue

Let me know if it worked or if you need anymore help.

让我知道它是否有效或者您是否需要帮助。

#2


0  

You can do this in following way too :

您也可以通过以下方式执行此操作:

List<WebElement> element= driver.findElements(By.xpath("//span[@class='_soakw coreSpriteLikeHeartOpen']"));

if(element.size()>0)
{
    System.out.println("Found");
}
else
{
        System.out.println("Not Found");            
}

Important :

FindElement() : It throws a NoSuchElementException exception when it fails to find If the element.

FindElement():当找不到元素时,它会抛出NoSuchElementException异常。

FindElements() : If the element doesn’t exist or not available on the page then, the return value will be an empty list.

FindElements():如果该元素在页面上不存在或不可用,则返回值将为空列表。

#1


1  

You can place a try catch block around the find element. After the catch blok the execution of the code wil continue. The function findelement throws a NoSuchElementException when there is no element found.

您可以在find元素周围放置一个try catch块。在捕获blok之后,代码的执行将继续。当没有找到元素时,函数findelement抛出NoSuchElementException。

// Set the timeout for searching an element
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try 
{
    // Try to find the element
    driver.findElement(By.xpath("(//span[@class='_soakw coreSpriteLikeHeartOpen'])")).click();
} 
catch (NoSuchElementException e)
{
    System.out.println("Element Not Found");
}
// Continue

Let me know if it worked or if you need anymore help.

让我知道它是否有效或者您是否需要帮助。

#2


0  

You can do this in following way too :

您也可以通过以下方式执行此操作:

List<WebElement> element= driver.findElements(By.xpath("//span[@class='_soakw coreSpriteLikeHeartOpen']"));

if(element.size()>0)
{
    System.out.println("Found");
}
else
{
        System.out.println("Not Found");            
}

Important :

FindElement() : It throws a NoSuchElementException exception when it fails to find If the element.

FindElement():当找不到元素时,它会抛出NoSuchElementException异常。

FindElements() : If the element doesn’t exist or not available on the page then, the return value will be an empty list.

FindElements():如果该元素在页面上不存在或不可用,则返回值将为空列表。