显式等待Selenium Webdriver中的findElements

问题描述 投票:2回答:2

登录后,页面重定向到一个页面(我想等待页面加载),我在那里通过tagName查找元素,

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

在这里,我想给明确等待findElements,我想等待它的所有可见性或存在。我的网页只有两个输入。如果我给Implicit Wait很长时间,代码就可以了。但它有所不同。所以我决定给出显式等待,我怎样才能明确等待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");


    }

}

请注意:如果我在代码“driver.navigate()。to(baseUrl +”/ anglr / form-b​​uilder / dist /#/ forms“);”之后给了Thread.sleep很长时间,“我将获得所有WebElements。但我想避免这种情况,我想等待WebElements加载()。

任何人请帮助。

selenium selenium-webdriver
2个回答
4
投票

你可以这样做:

//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:标题包含

0
投票

我决定弄清楚如何解决我的用例,我的用例想要对特定的FindElements调用进行明确的等待,但不是全部,并且摆弄Implicit Waits感觉不洁和可怕。所以我为IWebDriver实现了一个扩展方法,它使用WebDriverWait(参见Selenium中的Explicit Waits)来实现它。

    /// <summary>
    /// Allows you to execute the FindElements call but specify your own timeout explicitly for this single lookup
    /// </summary>
    /// <remarks>
    /// If you want no timeout, you can pass in TimeSpan.FromSeconds(0) to return an empty list if no elements match immediately. But then you may as well use the original method
    /// </remarks>
    /// <param name="driver">The IWebDriver instance to do the lookup with</param>
    /// <param name="findBy">The By expression to use to find matching elements</param>
    /// <param name="timeout">A timespan specifying how long to wait for the element to be available</param>
    public static ReadOnlyCollection<IWebElement> FindElements(this IWebDriver driver, By findBy, TimeSpan timeout)
    {
        var wait = new WebDriverWait(driver, timeout);
        return wait.Until((d) =>
        {
            var elements = d.FindElements(findBy);
            return (elements.Count > 0)
                ? elements
                : null;
        });
    }
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.