Reputation: 155
UPDATED VERSION
I'm trying to find a more dynamic way to wait for elements instead of using static wait functions like Task.Event(2000).Wait();
The solution to this seems to be this:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[2]/div/input")));
But when I use this function, the "ExpectedConditions" always lights up red indicating that it: "does not exist in the current context".
I've placed the function in one of my testcases:
(I am using C#/Visual Studios, Project type: Classlibrary)
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class MyFirstTest
{
IWebDriver driver = new FirefoxDriver();
[Test]
public void WaitverifyTest()
{
driver.Navigate().GoToUrl("https://www.google.se/");
driver.Manage().Window.Maximize();
Task.Delay(4000).Wait();
driver.FindElement(By.XPath("//div[2]/div/input")).SendKeys("Selenium");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[2]/div/input")));
element.Click();
driver.FindElement(By.XPath("//center/input")).Click();
}
}
}
(The xpath is a location type xpath, it is valid and works in both Selenium IDE and in Visual Studios.)
Anyone has any idea what I'm doing wrong?
I have two suspicions:
According to selenium.io , ExpectedCondition was last updated in 3.1.0. Maybe it is no longer vaild. Is there someway to check this?
Upvotes: 3
Views: 11854
Reputation: 103
I resloved this way:
1: Using nuget, search for DotNetSeleniumExtras.WaitHelpers,
2: Import that namespace into your class.
using SeleniumExtras.WaitHelpers
3: Then run the following code:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("element ID")));
Upvotes: 6
Reputation: 196
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.
Upvotes: 18
Reputation: 445
I don't understand this line:
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[2]/div/input")));
How are you able to cast Until to an IWebElement since it returns bool? I might be missing something.
But you might be able to bypass the error if you don't use ExpectedConditions, like so:
wait().Until( foo => driver.FindElement(By.XPath("//div[2]/div/input")).Enabled);
WebDriverWait takes as a parameter a function that returns a bool. You can create one within the parameters with the above code, that will return true when the element is enabled.
Upvotes: 1
Reputation: 4739
Instead of VisibilityOfAllElementsLocatedBy
use elementTobeClickable
method, it should work i guess
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
driver.Navigate().GoToUrl("https://www.svt.se/");
driver.Manage().Window.Maximize();
wait.Until(ExpectedConditions.ElementToBeClickable(
By.XPath("//li[4]/a")));
driver.FindElement(By.XPath("//li[4]/a")).Click();
Upvotes: -1