Explicit wait condition using C#

123 Views Asked by At

Is it possible to pass a IWebElement variable to an explicit wait condition instead of hard coding xpath values? Here is an example of what I'm trying to achieve. I would like to pass grant Email.GMSName variable as opposed to hard coding the xpath: /html/body/table/thead/tr/th[2]

try
{
    WebDriverWait w = new WebDriverWait(Driver, TimeSpan.FromSeconds(20));
  
    w.Until(ExpectedConditions.ElementExists(By.XPath(grantEmail.GMSName));

    Assert.IsTrue(grantEmail.GrantNumber.Displayed);
    Assert.IsTrue(grantEmail.GMSName.Displayed);
    Assert.IsTrue(grantEmail.PIName.Displayed);
    Assert.IsTrue(grantEmail.Institution.Displayed);
    Assert.IsTrue(grantEmail.Title.Displayed);
    Assert.IsTrue(grantEmail.PIReqAmt.Displayed);
    Assert.IsTrue(grantEmail.DIVRecAmount.Displayed);
    Assert.IsTrue(grantEmail.GMSRecAmount.Displayed);
    Assert.IsTrue(grantEmail.GMORecAmount.Displayed);
    Assert.IsTrue(grantEmail.DIVRank.Displayed);
    Assert.IsTrue(grantEmail.RFAPA.Displayed);
    Assert.IsTrue(grantEmail.PCC.Displayed);
    Assert.IsTrue(grantEmail.PercentileScore.Displayed);
    Assert.IsTrue(grantEmail.CouncilNPARS.Displayed);
    Assert.IsTrue(grantEmail.NPARSProgramReqNumberofYears.Displayed);
    Assert.IsTrue(grantEmail.CANPoolcode.Displayed);
    Assert.IsTrue(grantEmail.BatchNumber.Displayed);
}
1

There are 1 best solutions below

0
Prophet On

You can pass a IWebElement object as parameter to WebDriverWait ExpectedConditions.
Similarly to syntax you presented in the question

WebDriverWait w = new WebDriverWait(Driver, TimeSpan.FromSeconds(20));
w.Until(ExpectedConditions.ElementExists(By.XPath(grantEmail.GMSName));

where you passing a grantEmail.GMSName XPath expression.
You can pass a IWebElement object, as following:

WebDriverWait w = new WebDriverWait(Driver, TimeSpan.FromSeconds(20));
w.Until(ExpectedConditions.ElementToBeClickable(grantEmailElement);

where

IWebElement grantEmailElement = Driver.findElement(By.XPath(grantEmail.GMSName));

Here you can see all the available ExpectedConditions.
As you can see, some of them accepting By as parameter and others IWebElement.