2015-12-21 118 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:标题包含