SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.63k stars 8.18k forks source link

[šŸ› Bug]: Script is not running without static wait - Thread.sleep() #12250

Closed aarahmanqa closed 1 year ago

aarahmanqa commented 1 year ago

What happened?

The program attached is not working unless we use static wait. I want to know whats wrong with this web page that Selenium is not able to handle it?

https://stackoverflow.com/questions/76545949/selenium-program-not-running-without-static-wait/76545967#76545967

How can we reproduce the issue?

public class Eljur {
    private static WebDriver driver;
    private static final Duration TIMEOUT_DURATION = Duration.ofSeconds(30);

    public static void main(String[] args) {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://eljur.ru/login");
        customWait(1);
        findElement(By.cssSelector("div.form-select-button__placeholder")).click();
        findElement(By.cssSelector("input.form-input__input")).sendKeys("Y");
        findElements(By.cssSelector("div.form-select-option")).get(0).click();
        driver.quit();
    }

    public static void customWait(int seconds) {
        try {
            Thread.sleep(seconds * 1000L);
        } catch (Exception ignored) {
        }
    }

    public static WebElement findElement(By by) {
            return waitFor(elementToBeClickable(by));
    }

    public static List<WebElement> findElements(By by) {
            return waitFor(numberOfElementsToBeMoreThan(by,0));
    }

    public static<T> T waitFor(Function<WebDriver,T> function) {
            return new WebDriverWait(driver, TIMEOUT_DURATION)
                    .until(function);
    }
}

Relevant log output

The program is not throwing any error at all when its not clicking. The next step fails as that element is available.

Operating System

Mac

Selenium version

4.10.0

What are the browser(s) and version(s) where you see this issue?

Latest Chrome

What are the browser driver(s) and version(s) where you see this issue?

LatestChrome

Are you using Selenium Grid?

No

github-actions[bot] commented 1 year ago

@aarahmanqa, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

titusfortner commented 1 year ago

This isn't a bug that a change in Selenium code can fix.

Any site with JavaScript can display an element that can be clicked, but does not yet have the event listener attached for that click, so the expected action does not take place. Knowing the underlying dev code helps debug this; I've also just added retry loops in the past based on a provided conditional.

github-actions[bot] commented 1 year ago

šŸ’¬ Please ask questions at:

aarahmanqa commented 1 year ago

@titusfortner Thanks for the clarification. Following up my previous question, can you please tell me is there any way we shall wait for that event to get attached to that button?

Similar to isEnabled(), do we have any Selenium methods or even javascript methods exist to wait for such event to get attached?

Also, retry logic over here is not a good practise, isnt it? for 0 to 3 click on the element if next element appears break else sleep(2000)

If we have to adopt this way, we would have to write code like this everywhere.

titusfortner commented 1 year ago

Devs would need to add something to front end code to indicate the listener is attached. No way to guess, unfortunately. I'm not familiar enough with chrome devtools, but might be a way with network module.

github-actions[bot] commented 10 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.