I'm trying to optimize selenium tests and wanted to control the poll interval better so I decided to use DefaultWait that allows access to defined the polling period.
However, an unanticipated problem occurred - IgnoreExceptionTypes seems not to work or I'm doing something incorrectly. It fails immediately, not when the Timeout expires.
I have tried:
new WebDriverWait(driver, tss).Until(ExpectedConditions.TitleContains("Certificate Error"));
- worked well for me, but I want the poll to be smaller than 500ms, for precision;
Providing different exception types;
I have removed new WebDriverWait
so that only DefaultWait
would exist, apparently, since people had problems mixing up different wait objects, see here: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/8062
If I add Task.Delay(3000).Wait();
before the first wait.Until what throws the exception, then it does not throw it anymore (since the page is able to load and element is present), however subsequent waits fail in the same way.
Let me know what might be the cause of this, am I missing something?
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace TestProject1
{
class Program1
{
static void Main(string[] args)
{
// Initialize IWebDriver parameters
var service = InternetExplorerDriverService.CreateDefaultService();
service.LogFile = @"C:\Users\cbfdbfd\Desktop\IE.log";
service.LoggingLevel = InternetExplorerDriverLogLevel.Debug;
// Initialize the IWebDriver
IWebDriver driver = new InternetExplorerDriver(service);
// Initialize the DefaultWait<>
DefaultWait<IWebDriver> wait = new DefaultWait<IWebDriver>(driver);
wait.Timeout = TimeSpan.FromSeconds(50);
wait.PollingInterval = TimeSpan.FromMilliseconds(50);
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
driver.Navigate().GoToUrl("https://dfbfd/login");
wait.Until<bool>((d) => {
return d.Title.Contains("Certificate Error");
});
driver.Navigate()
.GoToUrl("javascript:document.getElementById('overridelink').click()");
// handle login
var username = driver.FindElement(By.Name("username"));
var password = driver.FindElement(By.Name("password"));
username.Clear(); username.SendKeys("fdbgfbf");
password.Clear(); password.SendKeys("bgfbgfb");
driver.FindElement(By.Id("login-btn")).Click();
//Task.Delay(3000).Wait();
// open supply chain profiles
wait.Until<IWebElement>((d) => {
return d.FindElement(By.LinkText("fgbgfbgfbgf"));
});
driver.FindElement(By.LinkText("gfbgfbgf")).Click();
// close driver
Task.Delay(3000).Wait();
driver.Close();
}
}
}
Debug log:
D 2017-06-29 02:39:33:656 Browser.cpp(513) Not in navigating state
D 2017-06-29 02:39:33:661 server.cc(355) Response: {"sessionId":"50577ecf-b46c-40a4-a2e0-ebb61a618d88","status":0,"value":null}
D 2017-06-29 02:39:33:666 server.cc(281) Command: POST /session/50577ecf-b46c-40a4-a2e0-ebb61a618d88/element {"using":"link text","value":"Supply Chain"}
D 2017-06-29 02:39:33:667 command.cc(36) Raw JSON command: { "name" : "findElement", "locator" : { "sessionid" : "50577ecf-b46c-40a4-a2e0-ebb61a618d88" }, "parameters" : {"using":"link text","value":"Supply Chain"} }
D 2017-06-29 02:39:33:667 IECommandExecutor.cpp(544) No alert handle is found
D 2017-06-29 02:39:33:667 ElementFinder.cpp(60) Using FindElement atom to locate element having linkText = Supply Chain
I 2017-06-29 02:39:33:667 Browser.cpp(130) No child frame focus. Focus is on top-level frame
D 2017-06-29 02:39:33:755 VariantUtilities.cpp(100) Result type is JScriptTypeInfo
W 2017-06-29 02:39:34:014 response.cc(77) Error response has status code 7 and message 'Unable to find element with link text == Supply Chain' message
D 2017-06-29 02:39:34:014 server.cc(355) Response: {"sessionId":"50577ecf-b46c-40a4-a2e0-ebb61a618d88","status":7,"value":{"message":"Unable to find element with link text == Supply Chain"}}
D 2017-06-29 02:39:34:563 ElementRepository.cpp(113) Refreshing managed element cache. Found 0 to remove from cache.
Stack trace:
OpenQA.Selenium.NoSuchElementException occurred
HResult=0x80131500
Message=Unable to find element with link text == Supply Chain
Source=WebDriver
StackTrace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByLinkText(String linkText)
at OpenQA.Selenium.By.<>c__DisplayClass6.<LinkText>b__4(ISearchContext context)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
at TestProject1.Program.<>c.<Main>b__0_1(IWebDriver d) in C:\Users\bernam\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 90
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
I had the same problem when trying to find a single element. In the end I worked around the issue by using the following code; replacing FindElement with FindElements and checking if one element was found.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.PollingInterval = TimeSpan.FromSeconds(0.5);
wait.IgnoreExceptionTypes(typeof(NotFoundException), typeof(NoSuchElementException));
wait.Until(drv =>
{
var elementList = drv.FindElements(by);
return (elementList.Count == 1);
});
User contributions licensed under CC BY-SA 3.0