0

Why Selenium Expected Conditions of waitForElementToBeClickable doesn't come with built-in click() method?
Selenium knows I'm waiting for element to be clickable, so why should I have to perform additional action of driver.click() on that element?
They could at least add some kind of flag there so it will or not perform the click() when the Expected Condition is fulfilled.

Prophet
  • 32,350
  • 22
  • 54
  • 79
nuzooo
  • 133
  • 1
  • 11

1 Answers1

3

org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable(By locator) returns WebElement object, it is not a void. You can add the click() to custom void:

public static void waitForClickableAndClick(WebDriver driver, By by, int waitSeconds) {
    try {
        new WebDriverWait(driver, waitSeconds).until(ExpectedConditions.elementToBeClickable(by)).click();
    }
    catch (Exception exception) {
        exception.printStackTrace();
    }
}
pburgr
  • 1,722
  • 1
  • 11
  • 26
  • Still why it is not clicked? why I need the WebElement , I want to click on it eventualy – nuzooo Aug 10 '21 at 04:56
  • If your test does not perform the click, does your code throws any exception? Or what's the issue? – pburgr Aug 10 '21 at 06:39
  • There is no error, the question is just to elaborate why the selenium guys didn't add the `click()` to the `waitForElementToBeClickable` if anyhow what I want to do is click on the element at the time he will be clickable – nuzooo Aug 10 '21 at 15:50
  • It's not just about this case. You can observe similar void 'absence' in many other APIs (the source code would be enormously and unnecessarily big). It's on you (with SO community help) to be able to create custom methods you can use repeatedly even in upcoming projects. – pburgr Aug 11 '21 at 12:12
  • The question still on it's place, why they leave me the way to complete the method? – nuzooo Aug 11 '21 at 19:52
  • Imagine how many of current methods returns a WebElement object. If each of them would have implemented click method, the package size would grow enormously. And it's not just about the click. – pburgr Aug 12 '21 at 06:28
  • @pburgr you didn't understand the question. Author asked about `waitForElementToBeClickable` expected condition method. Not about any other expected condition. This method waits for element to be clickable. Why we are waiting for element to be clickable? Not to be presented or be visible, but clickable? To click it successfully. So, after fulfilling the `waitForElementToBeClickable` condition we are goinf to click on that element! Not extract it's text or do any other action. – Prophet Aug 13 '21 at 15:09
  • So, it would be just strait forward common sense to add a built-in `click()` inside the `waitForElementToBeClickable` method. or, at least, to give overloaded method getting 1 more attribute, some boolean flag, to include or exclude the built-in click there! This is what this question about. – Prophet Aug 13 '21 at 15:11
  • @pburgr these is exactly what Prophet wrote, now it is more explainable? – nuzooo Aug 13 '21 at 15:19
  • I see, same could be for the mentioned data/property extraction `.getRect()`, `.getAttribute(String attributeName)` after element is present or visible. Maybe those will come in next versions. I use to create or modify methods for functionalities I need (if not available from other source ofc). Thay way I learned so much. – pburgr Aug 17 '21 at 06:04