Intermittantly fails to find WebElement collection

Class library usage, coding and language questions.
markrussell
Posts: 7
Joined: Thu Jan 24, 2013 12:40 am

Intermittantly fails to find WebElement collection

Post by markrussell » Thu Feb 14, 2013 8:44 am

Hi team,

I'm a little puzzled by the following problem:
I have the following line of C# code which is meant to return a collection of webelements, using a "container" element which was acquired earlier:

IList<WebElement> _webElementList = container.Find<WebElement>("." + xpElement, 10000);

This only works intermittently. Sometimes it comes back straight away with the collection filled. Other times it times out returning a empty collection. By inspecting the path using:

string _containerPath = container.GetPath().ToString();

I note that the path is different on the occasions that the collection is not found. However if I execute the subsequent code, it works :

WebElement _container = _containerPath;
_webElementList = _container.Find<WebElement>("." + xpElement, 10000);


If I re-extract the path at this point I note the path has changed.
Can someone please advise what is going on.
Thanks
Mark

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Intermittantly fails to find WebElement collection

Post by Support Team » Sat Feb 16, 2013 7:51 pm

Hello Mark,

Could you please give us more information about your code and your environment?
Which version of Ranorex are you using? How did you define "container" and what is the RxPath of "xpElement" is it possible to get a Ranroex Snapshot file from this element?
Is it possible to get a little solution in order to reproduce this issue?
Thank you!

Regards,
Bernhard
.
Image

markrussell
Posts: 7
Joined: Thu Jan 24, 2013 12:40 am

Re: Intermittantly fails to find WebElement collection

Post by markrussell » Sat Mar 09, 2013 6:06 am

Hi Bernhard,

This problem is still haunting me. Hoping you or someone can help.

I using Ranorex 4.0 and I have reproduced the problem against a simple public website so you can see for yourself with the example code below. You can run this to reproduce the issue.

In summary I'm trying to loop through various items in a menu - essentially clicking on each menu item and then reading the contents of the drill down items. However it seems that once a click on an item I can no longer use the menu container to access the next item.

PLease run the code below to reproduce the problem. I really hoping someone can explain to me why the attempt to access _menuItem2 fails . And why my workaround works. And hopefully to provide a better solution.

Thanks.

Here is the code:


System.Diagnostics.Process.Start("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "http://www.pembroke.sa.edu.au/Introducing-Pembroke/");

WebElement _menuContainer = "/dom[@domain='www.pembroke.sa.edu.au']//ul[#'Menu2']";

WebElement _menuItem1 = _menuContainer.FindSingle<WebElement>("./li/a[@innertext='International Students']", 1000);
_menuItem1.PerformClick();

// Now check that menu container is still valid
bool _valid = _menuContainer.Valid;
bool _validateExists = Ranorex.Validate.Exists(_menuContainer, "Some message", false);

// This line below FAILS to find the element
WebElement _menuItem2 = _menuContainer.FindSingle<WebElement>("./li/a[@innertext='Location and Facilities']", 1000);

// Therefore I am using this workaround here
if (!_menuContainer.TryFindSingle<WebElement>("./li/a[@innertext='Location and Facilities']", 1000, out _menuItem2))
{
_menuItem2 = _menuContainer.FindSingle<WebElement>(_menuContainer.GetPath().ToString() + "/li/a[@innertext='Location and Facilities']", 1000);
_menuItem2.PerformClick();
}

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Intermittantly fails to find WebElement collection

Post by Support Team » Mon Mar 11, 2013 5:35 pm

Hi Mark,

This is because your webpage will be reloaded when you click on one of your ATags and this makes the
existing/old elements invalid.
Your workaround works because you researched for the element with the absolute path.

Regards,
Markus
.
Image

markrussell
Posts: 7
Joined: Thu Jan 24, 2013 12:40 am

Re: Intermittantly fails to find WebElement collection

Post by markrussell » Tue Mar 12, 2013 1:38 am

Thanks Markus,

It certainly doesn't appear that the web page is reloaded at all. You can tell when a webpage is being refreshed. Anyway I suspect it is just the left menu container that is being refreshed which I guess amounts to the same thing - so that makes sense.

When I run this in a program however, it fails quite often, because the menu has not yet been refreshed when I try to re-aquire the element.
If I put a physical sleep command before re-aquiring the menu element, it works OK. However I dont know how to make this efficient. If my internet is running quickly it may refresh the menu in .1 seconds, however on occasion it takes up to 10 seconds to refresh. And I don't want to wait 10 seconds between each menu click.

1. Is there a way of making this more efficient? In other words, how can I tell when the menu has been redrawn and I can then re-acquire it?

2. The following two lines of code:

bool _valid = _menuContainer.Valid;
bool _validateExists = Ranorex.Validate.Exists(_menuContainer, "Some message", false);

both return TRUE after the click when the menu container is 'invalid'. Why is that?

3. What is the difference between the above two lines of code?

Thanks
Mark

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Intermittantly fails to find WebElement collection

Post by Support Team » Wed Mar 13, 2013 4:50 pm

Hi Mark,

Sorry that my explanation was too vague.
If you execute the tests in Chrome Ranorex will not recognize these elements as invalid it will recognize them as invisible, in contrast to IE, after you perform the click on one of the menu items and as I can see all elements will be set to invisible after the click not just the ones from the menu.

There are no major differences between "Validate.Exists(Element)" and "Element.Visible", the validate method will just additionally report a specific message (throw an exception).

The following code would be a way to handle this more efficient:
Validate.Options options = new Validate.Options(false, ReportLevel.Info);
options.ExceptionOnFail = false;
//waits until the new elements are applicable
while(!Validate.Attribute(_menuContainer, "Visible", false, "Waits until attribute is set to false", options)){
				Delay.Duration(500);
}
Regards,
Markus
.
Image