selenium webdriver - ChromeDriver.findElements() always takes 2 seconds when the target element is not found - Stack Overflow

I want to instantly determine if an element is present in the page. I wrote this simple Selenium Java c

I want to instantly determine if an element is present in the page. I wrote this simple Selenium Java code. The driver is an instance of ChromeDriver.

By by = By.id("non existent element");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(0));
List<WebElement> elementList = driver.findElements(by);
if (!elementList.isEmpty()) {
    // do something with the element.
}
// element is not present.

The problem is that when the element is not present, driver.findElements() always takes 2 seconds to execute, even with the implicit wait set to 0.

Here is the markup of the page being searched, which is quite small.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Error Occurred</title>
    <link href="/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="d-flex justify-content-center align-items-center vh-100 bg-light">
<div class="text-center">
    <h1 class="display-1 text-danger">Oops!</h1>
    <h2 class="text-dark">An Error Has Occurred</h2>
    <p class="text-muted">We’re sorry, but something went wrong. Please try again later.</p>
    <a href="login.html" class="btn btn-primary mt-3">Return to Login</a>
</div>
<script src="/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Any thoughts on how make this instant?

Thanks!

I want to instantly determine if an element is present in the page. I wrote this simple Selenium Java code. The driver is an instance of ChromeDriver.

By by = By.id("non existent element");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(0));
List<WebElement> elementList = driver.findElements(by);
if (!elementList.isEmpty()) {
    // do something with the element.
}
// element is not present.

The problem is that when the element is not present, driver.findElements() always takes 2 seconds to execute, even with the implicit wait set to 0.

Here is the markup of the page being searched, which is quite small.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Error Occurred</title>
    <link href="https://cdn.jsdelivr/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="d-flex justify-content-center align-items-center vh-100 bg-light">
<div class="text-center">
    <h1 class="display-1 text-danger">Oops!</h1>
    <h2 class="text-dark">An Error Has Occurred</h2>
    <p class="text-muted">We’re sorry, but something went wrong. Please try again later.</p>
    <a href="login.html" class="btn btn-primary mt-3">Return to Login</a>
</div>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Any thoughts on how make this instant?

Thanks!

Share Improve this question edited Mar 26 at 18:43 JeffC 25.9k5 gold badges34 silver badges55 bronze badges asked Mar 26 at 18:07 DiliDili 6261 gold badge7 silver badges15 bronze badges 5
  • 2 How large is the page? Searching large object collections takes time. – Tim Roberts Commented Mar 26 at 18:08
  • The page's markup is small. I have updated my question with it. – Dili Commented Mar 26 at 18:09
  • the default is already 0, so not sure why you'd set the implicitwait time there. If you've turned on implicitwaits that would probably add 0.5 seconds wait time as a wait is a polling loop with 1/2 second sleep inside. There will also be some time to go through the entire DOM to check for all elements that match the locator. The 2 seconds you are seeing is probably a combination of those things + the amount of time it takes for Selenium to wait for a standard page load. (by default that is the time for sending request, receiving headers, DOM to be loaded + any outside resources... ) – browsermator Commented Mar 27 at 20:44
  • in this case also the added time for the server to error out and start returning it's generic error page. – browsermator Commented Mar 27 at 20:45
  • We can't give a definitive answer until we see where you've started and stopped your timer. Can you include your full code? – browsermator Commented Mar 27 at 20:50
Add a comment  | 

1 Answer 1

Reset to default 1

This happens because ChromeDriver has an internal timeout for findElements(), even when implicit wait is set to 0.

Solution:

Use driver.executeScript() to check for element presence without waiting:

boolean isPresent = (boolean) ((JavascriptExecutor) driver)
    .executeScript("return document.getElementById('non existent element') !== null;");

This avoids unnecessary delays from Selenium’s find mechanisms.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744133609a4559952.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信