I'm using Selenium Webdriver with C# and I'm wondering if there's a way to override the FindElement method? What I'd like to do - if possible - is to add an extra parameter and code to the method that would force it to wait for the element to be visible before proceeding.
For example, it would then be something like this:
Driver.FindElement(By.Id("orion.dialog.box.ok"), 60).Click();
This would wait up to 60 seconds for the element to appear and be available to click.
Any ideas how to do this?
Thanks,
John
You could use ImplicitWait for this. You can create a new user defined function to accept the By object and the timeout seconds and have the function return the IWebElement, something like below:
IWebElement elem = MyOwnGetElement(By.id("test"),60);
The above function might have the below code, where time_out_sec is the function parameter.
WebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(time_out_sec));
driver.Url = "http://somedomain/url_that_delays_loading";
IWebElement myDynamicElement = driver.FindElement(By.Id("someDynamicElement"));
I would suggest you add it as an extension method. Pseudo code:
public static IWebElement WaitForAndFindElement(this IWebDriver driver, By by)
{
// do a wait
// something like...
WebDriverWait wait = new WebDriverWait(TimeSpan.FromSeconds(60)); // hard code it here if you want to avoid each calling method passing in a value
return wait.Until(webDriver =>
{
if (webDriver.FindElement(by).Displayed)
{
return webDriver.FindElement(by);
}
return null; // returning null with force the wait class to iterate around again.
});
}
Just to state, an implicit wait is going to be part one of your solution. It will cause Selenium to wait up to 60 seconds for an element to be present in DOM but being visible is something entirely different.
.Displayed will handle that, and it must be handled within a WebDriverWait.
I am coding with Selenium for 6+ months and I had the same problem as yours. I have created this extension method and it works for me every time.
What the code does is:
During 20 seconds, it checks each 500ms, whether or not the element is present on the page. If after 20 seconds, it's not found, it will throw an exception.
This will help you make a dynamic wait.
public static class SeleniumExtensionMethods {
public static WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
public static void SafeClick(this IWebElement webElement) {
try {
wait.Until(ExpectedConditions.ElementToBeClickable(webElement)).Click();
} catch (TargetInvocationException ex) {
Console.WriteLine(ex.InnerException);
}
}
and then use it like this:
IWebElement x = driver.FindElement(By.Id("username"));
x.SafeClick();
Related
I'm doing unit testing on a system using Selenium with a Page Object Model.
Usually I'm able to figure out a solution to such a problem in relatively short order, but this one is being rather tenacious, so I thought I should just get clarification on the root of the problem rather than write another workaround.
There is some PageObject class that has an IWebElement property with the necessary FindsBy attribute for locating a element on the page, and a method that uses said property:
public class Foo : BasePage
{
...
[FindsBy(How = How.XPath, Using = "//a[#title='Edit']")]
protected IWebElement LinkEdit { get; set; }
...
public Bar ClickEdit()
{
WaitUntilInteractable(LinkEdit);
LinkEdit.Click();
return new Bar(driver);
}
...
}
Note that this is the only reference to LinkEdit
For clarity, WaitUntilInteractable is defined in BasePage as:
protected void WaitUntilInteractable(IWebElement webElement)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
_ = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(webElement));
}
My understanding of how the PageFactory works with Selenium is that whenever you reference a IWebElement property of a class, the IWebDriver will then find the element within the page.
This much is said on this page: https://github.com/seleniumhq/selenium/wiki/pagefactory
... every time we call a method on the WebElement, the driver will go and find it on the current page again.
Given that knowledge, I don't quite understand how I can run into StaleElementReferenceException when simply referencing one of these properties. Surely either the element cannot be found, in which case a NoSuchElementException will be thrown, or it can be found, in which case it is present on the document, and not "stale".
Perhaps I've misunderstood staleness.
A StaleElementException occurs when the element being reference is no longer attached to the HTML document. This can happen in amazingly small amounts of time, and generally occurs when JavaScript is updating the page. As a further frustration, the WebDriverWait object does not catch StaleElementException's and continue waiting. You must tell WebDriverWait to ignore these exceptions:
protected void WaitUntilInteractable(IWebElement webElement)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.IgnoreExceptionTypes(typeof(StaleElementException));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(webElement));
}
Now when webElement is stale, it retries the operation until the wait object times out.
I am trying to automate the Internet Explorer in c# using selenium-webdriver to fill a formular on an external website.
Sometimes the code throws randomly errors (Unable to find element with name == xxx), because it cannot find the searched elements. It's not happening every time and not necessarily in the same places.
I've already tried setting an implicitWait with the following code which reduced the number of errors.
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
The external webpage changes the selection options (through reloading) from Dropdowns after selecting an option from another Dropdown.
To additionally intercept these critical points, I wait 2 seconds before trying to find the Dropdown-options ByName().
System.Threading.Thread.Sleep(2000);
The webpage takes less than half a second to reload this Dropdowns, so that 2 seconds should be enough.
Can you tell me what i'm doing wrong or why the webdriver seems to run so instably finding elements.
I have also noticed that I am not allowed to do anything else on the computer as long as the program is running, otherwise the same error occurs more often.
My driver-options for internet explorer 8
var options = new InternetExplorerOptions()
{
InitialBrowserUrl = "ExternalPage",
IntroduceInstabilityByIgnoringProtectedModeSettings = true,
IgnoreZoomLevel = true,
EnableNativeEvents = false,
RequireWindowFocus = false
};
IWebDriver driver = new InternetExplorerDriver(options);
driver.Manage().Window.Maximize();
My final solution working perfectly without a single error in more than 20 tests!
Added the following in the class
enum type
{
Name,
Id,
XPath
};
Added this behind my driver
driver.Manage().Window.Maximize();
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
Methode to wait for an element
private static void waitForElement(type typeVar, String name)
{
if( type.Id)wait.Until(ExpectedConditions.ElementToBeClickable(driver.FindElement(By.Id(name))));
else if(type.Name)wait.Until(ExpectedConditions.ElementToBeClickable(driver.FindElement(By.Name(name))));
else if(type.XPath)wait.Until(ExpectedConditions.ElementToBeClickable(driver.FindElement(By.XPath(name))));
}
And call the methode before calling any event with the element
waitForElement(type.Id, "ifOfElement");
You can use Explicit wait like this example:
var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("your locator")));
You have two more options in Selenium:
You can use explicit wait via the WebDriverWait object in Selenium. In this way you can wait for elements to appear on the page. When they appear the code continues.
An example:
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://yourUrl.com");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
Func<IWebDriver, bool> waitForElement = new Func<IWebDriver, bool>((IWebDriver Web) =>
{Console.WriteLine(Web.FindElement(By.Id("target")).GetAttribute("innerHTML"));
});
wait.Until(waitForElement);
Furthermore you can use the FluentWait option. In this way you can define the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});
the html
<span id="banner">Rolling in 10.00...</span>
my code
public void waitroller()
{
Console.WriteLine("waiting roller");
WebDriverWait wait = new WebDriverWait(PropertiesCollection.driver, TimeSpan.FromDays(2));
wait.Until<IWebElement>((d) =>
{
IWebElement element = PropertiesCollection.driver.FindElement(By.Id("banner"));
if (element.Text == "Rolling in 20.00...")
{
return element;
}
return null;
});
}
OR
public void waitroller()
{
new WebDriverWait(PropertiesCollection.driver, TimeSpan.FromMinutes(1.5)).Until(ExpectedConditions.TextToBePresentInElementLocated(By.Id("banner") , "Rolling in 20.00..."));
}
I'm stuck with this innertext because the innertext in the html is always changing and i want to make a webdriverwait that waits until the innertext in the span banner is "Rolling in 20.00" . Is there any way to make custom expected conditions because I am not understand custom expected condition.
what i need is something like this
new WebDriverWait(PropertiesCollection.driver, TimeSpan.FromMinutes(1.5)).Until(ExpectedConditions.TextToBePresentInElement(PropertiesCollection.driver.FindElement(By.Id("banner"), "Rolling in 20.00...");
but this is not work because i think the id="banner" is innerText
there is one way that work but can't work for long because i think it is the wrong way of doing it . i loop the element banner many times and check if the innerText is equals to "Rolling in 20.00..." but have error also
public void waitroller()
{
Console.WriteLine("waiting roller");
WebDriverWait wait = new WebDriverWait(PropertiesCollection.driver, TimeSpan.FromDays(2));
IWebElement bannerElement = PropertiesCollection.driver.FindElement(By.Id("banner"));
wait.Until((d) =>{ return bannerElement.Text.Contains("Rolling in 20.00"); });
}
I don't know what you mean by "innertext", but with Selenium's WebDriverWait you can wait for whatever you want. You don't have to use the ExpectedConditions, they're only for programmers' comfort.
Example:
new WebDriverWait(...).Until(driver => driver.FindElement(...).Text.Equals(...));
On the other hand, your problem might be a completely different one. Is your "roller" a countdown where the text changes very fast? In this case you might never reach the expected condition. I think Selenium checks the expected conditions every 500 milliseconds, so it doesn't even notice when "Rolling in 20.00" is displayed for just let's say 50 millisecods and then has already changed to "Rolling in 19.95" when Selenium checks for the next time.
I have setup a class with properties get value from WebDriver.FindElements().
public IList<IWebElement> ListObjectElements
{
get
{
var container = WebDriver.FindElement(By.Id("objects"));
return container.FindElements(By.XPath("//*[contains(#id, 'id_')]"));
}
}
I also implemented test case to test Add New function.
All steps ran success. When I tried to get the return of new list after Add New, it missed 1 item.
I set a break point to watch value. The property ListObjectElements has 10 items, but the return newList only has 9.
var newList = clientpage.ListObjectElements;
return newList;
If I add a Thread.Sleep(), the return newList has 10 items same as the property ListObjectElements.
How could I get the exactly results without using Thread.Sleep()?
Thanks in advance for you help.
It sounds like the site you're automating adds the elements representing the objects dynamically to the DOM, and that your code is then losing the race condition that you execute FindElements before the elements are actually added to the DOM. You'll need to implement some sort of wait in your code. You might be able to leverage the WebDriverWait construct, the .NET implementation of which is available in the WebDriver.Support assembly. The construct could be used in a manner similar to the following:
// Assumes 'driver' is a valid IWebDriver instance.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until<bool>((d) =>
{
return clientPage.ListObjectElements.Count > 9;
});
Putting this in a more concrete context in your test case would look something like this, depending on your architecture and desires:
// Assume that the element you click on to add a new element
// is stored in the variable 'element', and your IWebDriver
// variable is 'driver'.
int originalCount = clientPage.ListObjectElements.Count;
element.Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until<bool>((d) =>
{
return clientPage.ListObjectElements.Count > originalCount;
});
return clientPage.ListObjectElements;
I am really sorry if this question has already been asked/answered. but I could not find it.
Please excuse my ignorance as I am new to WebDriver.
When the page is initially loads, it displays a LOADING DIV untill all the data is loaded. How can I wait until this div is hidden before I proceed with other actions on page elements?
I am trying to know as follows:
public static void waitForPageLoad(string ID, IWebDriver driver)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Id(ID));
});
}
I pass the Id of SOME OTHER ELEMENT to this function which I will use when the LOADING DIV disappears. It returns the wrong result as the element by ID is actually present/loaded but is behind the grey DIV that shows "Loading... Please wait" message. So this does not work. I would like to know when that LOADING div disappears.
Any help is greatly appreciated.
By waiting on a bool value instead of IWebElement, the .NET WebDriverWait class will wait until a value of true is returned. Given that, how about trying something like the following:
public static void WaitForElementToNotExist(string ID, IWebDriver driver)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until<bool>((d) =>
{
try
{
// If the find succeeds, the element exists, and
// we want the element to *not* exist, so we want
// to return true when the find throws an exception.
IWebElement element = d.FindElement(By.Id(ID));
return false;
}
catch (NoSuchElementException)
{
return true;
}
});
}
Note that this is the appropriate pattern if the element you're looking for is actually removed from the DOM. If, on the other hand, the "waiting" element is always present in the DOM, but just made visible/invisible as required by the JavaScript framework your app is using, then the code is a little simpler, and looks something like this:
public static void WaitForElementInvisible(string ID, IWebDriver driver)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until<bool>((d) =>
{
try
{
IWebElement element = d.FindElement(By.Id(ID));
return !element.Displayed;
}
catch (NoSuchElementException)
{
// If the find fails, the element exists, and
// by definition, cannot then be visible.
return true;
}
});
}