Reputation: 749
I have a very simple requirement to load a web page, wait until it's finished loading, and then wait for an element to become present/visible (this only happens after the page has signaled that it finished loading) before doing anything else with the page.
I tried using the WebBrowser control, but struggled with finding a way to wait for the element.
I looked at using CefSharp but I was told the only way is to inject js, which I'm not all that keen on doing.
I also looked at Selenium WebDriver too and the WebDriverWait
appears to do exactly what I want, but the fact that it's not actually a control, cannot be hidden and requires browsers to be installed is a no-no for me.
Is there a way of doing this which does not involve using Thread.Sleep
in either WebBrowser, CefSharp or any other project I haven't mentioned yet?
EDIT: Some extra information...
I am using this for web automation. In this particular instance to login to a website (I'm not storing passwords, don't worry).
The particular website in question supports different authentication methods at a user level, so after entering the username it runs a js script which determines whether to then ask for the password or for something like phone auth, smartcard auth, etc. The js script does not fire the DocumentCompleted
event, which is what I'm struggling with at the moment.
The idea is that if it asks for smartcard or phone auth to keep the form/control with .Visible = false
the whole time and just show it if the user is set for username/password auth.
So depending on what happens after I automate entering the username, it will either ask for a password, smartcard or phone auth and I need to wait for the DOM to change so that one of those 3 elements becomes visible so I know what to do.
I dislike Selenium because it involves the user having to install browsers, it's hard/impossible to make it hidden, it's not an actual control I can drop on my form, etc etc.
Upvotes: 0
Views: 1204
Reputation: 21
I know this is pretty old, but I figured I would add an answer in case anyone runs into it as well.
I have a similar setup to you that requires authentication prior to running each test, so prior to moving on with a test I will wait for elements to load (without checking the DocumentCompleted()
method).
What I do is use a helper that takes in some condition, in my case it's Func<bool>
, then checks for this condition every time period x, until a timeout of y. Both X and Y are configurable, but they have a default of say 1 second and 10 seconds. You can make the implementation decision on whether or not to suppress exceptions inside the method, but I would recommend doing it so that ElementNotFound exceptions do not stop the execution.
The timing helper function definition note: time in milli-seconds.
public static bool WaitFor(Func<bool> condition, int timeout = 10000, int waitTimePeriod = 1000)
Examples of use would be:
Upvotes: 1