0

I'm trying to use FluentWait instead of sleep and this is my first practice. First of all and most importantly am I doing it right at all? Secondly I got through two elements so I thought it kind of works (PaymentMethod button and CheckOut button). Before I implemented FluentWait it wouldn't find them. And finally it won't find the third(backToDesktop button) element. Keeps throwing Element not visible, although I added the wait.ignore(ElementNotVisibleExcecption.class).

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(login.getDriver());
    wait.withTimeout(5, TimeUnit.SECONDS);
    wait.pollingEvery(250, TimeUnit.MILLISECONDS);
    wait.ignoring(NoSuchElementException.class);
    wait.ignoring(ElementNotVisibleException.class);

    WebElement paymentMethod = wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return login.getDriver().findElement(By.xpath("//*[@id='paymentMethodHolder']/div[1]/div[1]/button"));
        }
    });
    paymentMethod.click();
    System.out.println("FOUND PAYMENTMETHOD BUTTON");

    WebElement checkOut = wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return login.getDriver().findElement(By.xpath("//*[@id='checout-footer-buttons']/button[2]"));
        }
    });
    checkOut .click();
    System.out.println("FOUND KINNITA BUTTON");

    WebElement backToDesktop= wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return login.getDriver().findElement(By.className("modal-button-text"));
        }
    });
    backToDesktop.click();

    System.out.println("FOUND BACKTODESKTOP BUTTON");
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
R.R
  • 23
  • 1
  • 9
  • Is the element visible on UI? – Ajinkya Mar 22 '17 at 13:04
  • @xyz It closes the moment it gets to that part but yes , it should be visible. Little addition. This is what i have done and it seems to be working : wait.ignoring(NoSuchElementException.class, ElementNotVisibleException.class); wait.ignoring(WebDriverException.class); Does it mean i'm doing it the correct way or is it just a lucky coincidence?Thank you! – R.R Mar 22 '17 at 13:09
  • I think this kind of approach might not be working at all. Because now i'm getting element not visible exception although i have wait.ignoring(ElementNotVisibleException.class). – R.R Mar 22 '17 at 13:28
  • `wait.ignoring(ElementNotVisibleException.class)` means selenium will keep looking for element even if ElementNotVisibleException is thrown. It wont be able to fetch element if it is invisible, irrespective of what exception you add to ignore list. – Ajinkya Mar 22 '17 at 13:36
  • @xyz Yes i know it, but it throws this exception at the first moment it passes through second button that it found. It doesn't wait for 5 seconds that wait.withTimeout(5, TimeUnit.SECONDS); This exception is thrown immidiantly. – R.R Mar 22 '17 at 13:43
  • Is the button there? What does the DOM look like at that point? Does it throw the exception on the click, or the findElement? – Mark Lapierre Mar 22 '17 at 13:48
  • @MarkLapierre The button is there. I tried to put Thread.sleep(2000) and it finds it perfectly, which means the exception is thrown right after the second button click. I guess the wait.withTimeout() is not working at all or maybe my whole approach is incorrect? – R.R Mar 22 '17 at 13:55
  • What really confuses me is that it seems to be working. Because: if i change the approach to find the first button to : login.getDriver().findElement(By.xpath("//*[@id='paymentMethodHolder']/div[1]/div[1]/button")); it doesn't work and a NoSuchElementException is thrown. – R.R Mar 22 '17 at 14:02
  • I'm just guessing now, but maybe you shouldn't reuse waits? What happens if you change the timeout to longer than the entire process should take? E.g., 30 seconds? Or does it work if you re-initialize the wait before each `until`? – Mark Lapierre Mar 22 '17 at 14:28
  • Not sure why this was voted down. Would the voter care to comment? – Mark Lapierre Mar 22 '17 at 14:28
  • @MarkLapierre Yes i also thought that longer timeout might fix it but it didn't help as all the others suggestions... Thank you anyways for the support, JeffC's solution is really simple and works perfectly. – R.R Mar 23 '17 at 08:23

1 Answers1

0

FluentWait is a custom wait. You shouldn't need it in most cases. You should always start with a WebDriverWait and ExpectedConditions and if that doesn't work, then maybe investigate a FluentWait. My guess is that something simple like the below will work for you. This is just one example. You should look at all the different conditions you can wait for that are provided by ExpectedConditions. Probably the most common ones are waiting for an element to be visible or clickable.

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someId")));
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • If you found this (or any) answer useful, please upvote it. If this answered your question, please accept it as the answer. Thanks! – JeffC Mar 23 '17 at 13:02
  • I did. Altough "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score". – R.R Mar 23 '17 at 13:50
  • It's all good. I just like to share that info with people that are newer to the site (low rep). – JeffC Mar 23 '17 at 15:54