C# Selenium 'Expectedconditions Is Obsolete'

C# Selenium 'ExpectedConditions is obsolete'


How to resolve this with the latest version of Selenium.

Using NuGet, search for DotNetSeleniumExtras.WaitHelpers, and import that namespace into your class. Now you can do this:

var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
var element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("content-section")));

And the warning in the IDE will be gone.

The name 'ExpectedConditions' does not exist in the current context

The below will solve your problem

Make sure you've installed both the Selenium.Webdriver and Selenium.Support NuGet packages for your project. You'll need the Selenium.Support package to use ExpectedCondition

OR

Resolve this way:

1: Using nuget, search for DotNetSeleniumExtras.WaitHelpers,

2: Import that namespace into your class.

using SeleniumExtras.WaitHelpers

3: Then example of code code:

 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("element ID")));

Wait verify using ExpectedConditions does not work

I've seen this before.

Make sure you've installed both the Selenium.Webdriver and Selenium.Support NuGet packages for your project. You'll need the Selenium.Support package to use ExpectedConditions.

WaitForElementClickable/ Visible - Selenium C#

As I understood your element is visible and clickable when loading element on the screen but overlaying you element, also maybe you need to wait javascript to complete to click successfully.

You need get "loading circles" locator. For that open chrome devtools trigger "loading circles" to appear and press F8(pause) then you can find html of loading element.

Wait until loading element is disappeared:

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(8));
wait.until(ExpectedConditions.invisibilityOfElementLocated(loadingElementLocator);

Also you can check if javascript is complete :

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
bool jsLoaded = (bool)js.ExecuteScript("return (document.readyState == \"complete\" || document.readyState == \"interactive\")");

Here Java example:

new WebDriverWait(driver, timeoutSec).until(d ->
{
boolean ajaxComplete;
boolean jsReady;
boolean loaderHidden = false;

JavascriptExecutor js = (JavascriptExecutor) d;
jsReady = (boolean) js.executeScript("return (document.readyState == \"complete\" || document.readyState == \"interactive\")");;

try {
ajaxComplete = (boolean) js.executeScript("var result = true; try { result = (typeof jQuery != 'undefined') ? jQuery.active == 0 : true } catch (e) {}; return result;");
} catch (Exception ignored) {
ajaxComplete = true;
}

try {
loaderHidden = !d.findElement(loadElementLocator).isDisplayed();
} catch (Exception ignored) {}

return ajaxComplete && jsReady && loaderHidden;
});

Here your updated code:

public static void WaitForLoading(IWebDriver driver, int timeoutInSeconds)
{
new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds)).Until(d =>
{
Boolean ajaxComplete;
Boolean jsReady;
Boolean loaderHidden = false;

IJavaScriptExecutor js = (IJavaScriptExecutor)d;
jsReady = (Boolean)js.ExecuteScript("return (document.readyState == \"complete\" || document.readyState == \"interactive\")"); ;

try
{
ajaxComplete = (Boolean)js.ExecuteScript("var result = true; try { result = (typeof jQuery != 'undefined') ? jQuery.active == 0 : true } catch (e) {}; return result;");
}
catch (Exception)
{
ajaxComplete = true;
}
try
{
loaderHidden = !d.FindElement(By.ClassName("Loader__background")).Displayed;
}
catch (Exception) { }

return ajaxComplete && jsReady && loaderHidden;
});
}

How to use:

WaitForLoading(driver, 10);
myButton.Click();

Selenium C# WebDriver: Wait until element is present

Alternatively you can use an implicit wait:

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

An implicit wait is to tell WebDriver to poll the DOM for a certain
amount of time when trying to find an element or elements if they are
not immediately available. The default setting is 0. Once set, the
implicit wait is set for the life of the WebDriver object instance.

How to add custom ExpectedConditions for Selenium?

An "expected condition" is nothing more than an anonymous method using a lambda expression. These have become a staple of .NET development since .NET 3.0, especially with the release of LINQ. Since the vast majority of .NET developers are comfortable with the C# lambda syntax, the WebDriver .NET bindings' ExpectedConditions implementation only has a few methods.

Creating a wait like you're asking for would look something like this:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until<IWebElement>((d) =>
{
IWebElement element = d.FindElement(By.Id("myid"));
if (element.Displayed &&
element.Enabled &&
element.GetAttribute("aria-disabled") == null)
{
return element;
}

return null;
});

If you're not experienced with this construct, I would recommend becoming so. It is only likely to become more prevalent in future versions of .NET.



Related Topics



Leave a reply



Submit