Reputation: 580
I'm trying to use WebDriverWait to wait for an element in a panel to load
WebDriverWait wait = WebDriverWait(MyWebDriver, 10);
By completeButton = By.Id("completeButton");
// This waits on page level and loads the first which isn't always the correct panel.
// How can I wait from a given panel?
IWebElement button = wait.Until(drv => drv.FindElement(completeButton));
button.Click();
I have multiple panels and each creates the complete button when they are ready hence the need for the wait.
I am able to wait for elements on the entire page however i want to be able to wait for elements from another element.
looking at the documentation this is obviously wrong but is there something similar to this that i could do to wait for an element from a subpage, how have others got around this issue?
WebDriverWait wait = WebDriverWait(MyPanelSubElement, 10);
Upvotes: 3
Views: 1670
Reputation: 987
1.Create a WebElementWait class :
public class WebElementWait : DefaultWait<IWebElement>
{
public WebElementWait(IWebElement element, TimeSpan timeout) : base(element, new SystemClock())
{
this.Timeout = timeout;
this.IgnoreExceptionTypes(typeof(NotFoundException));
}
}
2.Now use it as :
//Find your target panel . You can use WebDriverWait to lookup this element
//while waiting on webdriver if required.
IWebElement panel = //some code to get your container panel ;
IWebElement targetButton = new WebElementWait(panel, TimeSpan.FromMilliseconds(10000)).Until(p => { return p.FindElement(By.Id("completeButton")); });
targetButton.Click();
Hope this helps!!
Upvotes: 1
Reputation: 2334
You can add the explicit wait for the panel section loading.Currently, ExpectedConditions
is deprecated from the OpenQA.Selenium.Support.UI
and newly added in SeleniumExtras.WaitHelpers
. Please include the below NuGet Package
NuGet Package Needs to be Added:
DotNetSeleniumExtras.WaitHelpers
Expected Conditions will be available in both OpenQA.Selenium.Support.UI
and SeleniumExtras.WaitHelpers
.In order to avoid the conflicts, you can assign the newly imported package in one variable and can be access the required method.
So you can do the import like this ( using SeleniumWaitHelper = SeleniumExtras.WaitHelpers;
) and ExpectedConditions can be accessed as SeleniumWaitHelper.ExpectedConditions
Sample Code:
Here, system will wait for 10 seconds until the MyPanelSubElement
becomes visible.
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
//DotNetSeleniumExtras.WaitHelpers NuGet package needs to be added
wait.Until(SeleniumWaitHelper.ExpectedConditions.ElementIsVisible(By.Id("Required Element ID")));
Edit:
You can try with some xpath in order to wait for the required panel content section loading.
I assume that you are having multiple panel with same Xapth. Consider the below sample HTML
Sample HTML:
<div class="panelContainer">
---------------------
---------------------
<button id="completeButton" class="btn">Complete</button>
-------------------
</div>
You can parameterize the panel index and change it based on the required panel condition.
Sample Code:
int panelIndex = 1;
By buttonPath = By.XPath("//div[@class='Panel']["+panelIndex+"]//*[@id='completeButton']");
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
wait.Until(SeleniumWaitHelper.ExpectedConditions.ElementIsVisible(buttonPath));
Suppose, If you want to wait for the second panel button loading, then change the panel index as 2.It will wait until the button load gets completed inside the second panel.
Upvotes: 0