2016-08-23 54 views
1

我在頁面上有一個按鈕,有時不會顯示。我想讓我的代碼繞過它,但它不適用於driver.findElement.isDisplayed()。我正在考慮ExpectedConditions.elementToBeClickable,但我不知道如何使它成爲布爾值。有些幫助嗎?(當然,條件是返回錯誤,因爲它不是布爾值)。我如何寫一個if語句來使按鈕可見?

WebDriverWait wait = new WebDriverWait(driver, 10); 
     if(ExpectedConditions.elementToBeClickable(By.xpath("html//body//div[5]//div//div//form//div//div[1]//div[3]//div//div//div//input")).){ 
      WebElement ex = driver.findElement(By.xpath("//*[@class='ng-pristine ng-untouched ng-valid'][@value='export'][@type='radio']")); 
      ex.click(); 
      WebElement in = driver.findElement(By.xpath("html//body//div[5]//div//div//form//div//div[1]//div[3]//div//div//div//input")); 
      in.click(); 
     }else{new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html//body//div[5]//div//div//form//div//div[1]//div[3]//div//div//div//input")));} 

'

+0

您可以總是轉換語句,拋出一個異常,而不是返回false到布爾語句與try/catch塊。 –

回答

1

findElement返回或者元素或拋出NoSuchElementException,所以決定元素可見有如果條件你應該嘗試使用findElements,而不是因爲它的回報WebElement或空列表的任一列表,所以你只需要檢查其size如下: -

List<WebElement> elements = driver.findElements(By.xpath("your xpath")); 

//Now check it size 
if(elements.size() > 0 && elements.get(0).isDisplayed()) 
{ 
    WebElement element = elements.get(0); 
    //Now do your further stuff with this element 
}