[英]Explicit Wait for findElements in Selenium Webdriver
登录后,页面重定向到一个页面(我想等待页面加载),我在其中通过 tagName 查找元素,
By inputArea = By.tagName("input");
List <WebElement> myIput = driver.findElements(inputArea);
在这里我想给 Explicit Wait for 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");
}
}
请注意:如果我在代码“driver.navigate().to(baseUrl+”/anglr/form-builder/dist/#/forms”);”之后长时间给 Thread.sleep,我将获得所有 WebElements。 但我想避免这种情况,我只想等待 WebElements 加载 ()。
任何人请帮助。
你可以这样做:
//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
: 标题包含我决定弄清楚如何解决我的用例,我的用例想要对特定的 FindElements 调用进行显式等待,但不是所有调用,并且摆弄隐式等待感觉不干净和可怕。 因此,我为 IWebDriver 实现了一个扩展方法,它使用 WebDriverWait(请参阅 Selenium 中的显式等待)来实现它。
/// <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;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.