I have this:
public static void ByLinkText(string text)
{
for (var i = 0; i < 50; i++)
{
try
{
Setup.Driver.FindElement(By.LinkText(text)).Click();
return;
}
catch (Exception)
{
}
}
}
(The weird code in the middle is selenium - lets just skip that, it's not the case here)
Okay, so I have this ByLinkText() method, what you don't see here, is that I repeat this method infinitely until this middle thingy will execute correctly and then hit that return after.
So: This middle code is NOT executing correctly, and I want to know why, so what am I doing? I put a breakpoint in the catch section. What is happening? Nothing (and ByLinkText() still keeps going infinitely!).
Now, you would tell me "Hey! That middle code is just not throwing anything!", but oh sweet wishes... That middle code can do two things: either throw something at me, or do it like it should (prove1 and prove2).
So finally, what is this all about? About breakpoints. As soon as I put my breakpoint on that return (right after magical code!) that code executes properly! HOW in the programical world is that possible, that breakpoint repairs my application!?
When breakpoints make your application behave it means that there is probably a timing issue with your code. Whenever you stop at a break point the stop introduces a delay that may let other processing go on without error.
This is why you sometimes see Thread.Sleep(N); thrown into code here and there. That's a coder throwing his/her arms up and saying "I don't know. Just put a Sleep in there."
In fact, give it a try. Put a Thread.Sleep(3000); where the break point goes and see what happens. I don't ever recommend that this be the solution but it could be a good test of the theory.
I think this has more to do with selenium than you seem to believe. Have a look at implicit and explicit waits for finding the element.
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
Another thing to try is to save the IWebElement result into a temp var, put on small sleep and then click it. Sometimes the browser ui can't handle the commands as fast as selenium issues them.
var el = Setup.Driver.FindElement(By.LinkText(text));
Thread.Sleep(750);
el.Click();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With