2017-03-22 111 views
0

我想使用FluentWait而不是睡眠,這是我的第一次練習。首先,最重要的是我做對了嗎?其次我通過了兩個元素,所以我認爲它有點作品(PaymentMethod按鈕和CheckOut按鈕)。在我實現FluentWait之前,它不會找到它們。最後它不會找到第三個(backToDesktop按鈕)元素。雖然我添加了wait.ignore(ElementNotVisibleExcecption.class),但仍然拋出Element不可見。硒FluentWait和元素不可見異常

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"); 
+0

該元素是否在UI上可見? – xyz

+0

@xyz關閉它到達該部分的時刻,但是,它應該是可見的。幾乎沒有。這是我所做的,它似乎工作:wait.ignoring(NoSuchElementException.class,ElementNotVisibleException.class); \t \t wait.ignoring(WebDriverException.class);這是否意味着我正在以正確的方式來做,還是隻是幸運的巧合?謝謝! –

+0

我認爲這種方法可能根本不起作用。因爲現在我得到元素不可見異常,雖然我有wait.ignoring(ElementNotVisibleException.class)。 –

回答

0

FluentWait是自定義等待。大多數情況下你都不需要它。你應該總是從WebDriverWaitExpectedConditions開始,如果這不起作用,那麼也許調查FluentWait。我的猜測是,像下面這樣簡單的東西會適合你。這只是一個例子。你應該看看ExpectedConditions提供的所有可以等待的條件。可能最常見的是等待元素可見或可點擊。

WebDriverWait wait = new WebDriverWait(driver, 10); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someId"))); 
+0

非常感謝你!簡單和完美的作品。 –

+0

如果您發現這個(或任何)答案有用,請立即投票。如果這回答了您的問題,請接受它作爲答案。謝謝! – JeffC

+0

我做到了。 Altough「感謝您的反饋!記錄下名聲低於15的人的投票,但不要更改公開顯示的帖子分數。」 –