2015-12-21 123 views
1

登錄後,頁面被重定向到一個頁面(我想等待頁面加載)顯式的等待,在那裏我發現通過標籤名的元素,在硒的webdriver findElements

By inputArea = By.tagName("input"); 
List <WebElement> myIput = driver.findElements(inputArea); 

在這裏,我想給顯式等待findElements,我想等待它的所有可見性或存在。我的網頁只有兩個輸入。如果我長時間給隱式等待,代碼將工作。但它有所不同。所以我決定給明確的等待,我怎樣才能明確地等待findElements ?.或者如何從列表中檢查第二個的可見性(列出myIput)。即myIput.get(1)。當我給像下面這樣的visibilityOfAllElements()時,它會拋出錯誤。

WebDriverWait waitForFormLabel = new WebDriverWait(driver, 30);  
By inputArea = By.tagName("input"); 
List <WebElement> myIput = driver.findElements(inputArea); 
waitForFormLabel.until(ExpectedConditions.visibilityOfAllElements(myIput)); 
myIput.get(1).sendKeys("Test"); 

這是我在自動化程序中使用的代碼列表。

package mapap; 
import java.util.ArrayList; 
import java.util.List; 

import lib.ReadExcellData; 
import lib.WriteExcellData; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.Select; 
import org.openqa.selenium.support.ui.WebDriverWait; 
import org.testng.annotations.BeforeClass; 
import org.testng.annotations.Test; 

import com.relevantcodes.extentreports.ExtentReports; 
import com.relevantcodes.extentreports.ExtentTest; 
import com.relevantcodes.extentreports.LogStatus; 

public class EditForm { 
    public static WebDriver driver; 
    static String excelName   = "D:\\xlsx\\map2.xlsx"; 
    ReadExcellData readData   = new ReadExcellData(excelName); 
    WriteExcellData writeData  = new WriteExcellData(excelName); 
    String baseUrl     = readData.getExcellData("base", 0, 0);  
    By colRadio; 
    ExtentReports report; 
    ExtentTest logger; 

    @BeforeClass 
    public void browserOpen() throws Exception{ 

     report = new ExtentReports("D:\\driver\\extentRepo\\Edit Map Forms.html", true); 
     logger = report.startTest("Map Forms Automation Testing", "Adding Forms"); 

     driver = new FirefoxDriver();  
     driver.get(baseUrl); 
     String username = readData.getExcellData("user", 1, 0); 
     String password = readData.getExcellData("user", 1, 1); 
     WebDriverWait waitForUserName = new WebDriverWait(driver, 250); 
     By usernameField = By.name("username"); 
     waitForUserName.until(ExpectedConditions.elementToBeClickable(usernameField)).sendKeys(username); 
     driver.findElement(By.name("password")).sendKeys(password); 
     driver.findElement(By.xpath("//input[contains(@src,'/images/signin.png')]")).click(); 

    } 

    @Test(priority = 1) 
    public void addingForm() throws Exception{  
      driver.navigate().to(baseUrl+"/anglr/form-builder/dist/#/forms"); 
     driver.manage().window().maximize();  
     WebDriverWait waitForFormLabel = new WebDriverWait(driver, 30);  
     By inputArea = By.tagName("input"); 
     List <WebElement> myIput = driver.findElements(inputArea); 
     waitForFormLabel.until(ExpectedConditions.visibilityOfAllElements(myIput)); 
     myIput.get(1).sendKeys("Test"); 


    } 

} 

請注意:如果我給了Thread.sleep的代碼之後很長一段時間 「driver.navigate()到(+的baseUrl」/ anglr /形式建設者/距離/#/表格 「);」 ,我會得到所有的WebElements。但我想避免這種情況,我只想等待WebElements加載()。

任何人請幫忙。

回答

3

你可以做這樣的事情:

//explicit wait for input field field 
WebDriverWait wait = new WebDriverWait(driver, 10); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("input"))); 

ExpectedConditions類可以在很多情況下是有用的,並提供了一些組預定義的條件等待元素。下面是其中一些的:

  • alertIsPresent:警報是本
  • elementSelectionStateToBe:元件狀態是選擇。
  • elementToBeClickable:元素存在並可點擊。
  • elementToBeSelected:選擇元素
  • frameToBeAvailableAndSwitchToIt:幀可用並選擇了幀。
  • invisibilityOfElementLocated:一個元素是不可見的
  • presenceOfAllElementsLocatedBy:存在的元素位於。
  • textToBePresentInElement:特定元素上存在的文本
  • textToBePresentInElementValue:和特定元素存在的元素值。
  • visibilityOf:可見的元素。
  • titleContains:標題包含