Repository finds element, user code does not

Ask general questions here.
Sean P
Posts: 5
Joined: Thu Jul 03, 2014 12:03 am
Location: Brea, Ca

Repository finds element, user code does not

Post by Sean P » Thu Jul 03, 2014 12:22 am

I have the following XPath in my repository:
RanorexEx1.JPG
It can be successfully found in the repository. See the highlighted element, and its screenshot

Now when I remove the a tag part of the path and in user code do the following:

Code: Select all

Ranorex.Unknown day = repo.CMTCMT.ExhibitionStartDay.FindSingle("/a[@innertext='10']");
day.Click();
Ranorex cannot find the path.
NoFound.JPG
Can I get a little help/insight here with why this isnt working? Ive tried playing around with different XPaths and every time the repo can find the element but when I got to pulling out the a[@innertext='somevalue'] part, it cant find the element via user code.

I have done a

Code: Select all

MessageBox.Show(repo.CMTCMT.ExhibitionStartDayInfo.Path.ToString() + "/a[@innertext='10']");
, and its exactly how the repository has it when it can find it via the Ranorex UI.

Thanks in advance.

Snapshot has been uplaoded
You do not have the required permissions to view the files attached to this post.

Bat
Posts: 16
Joined: Thu Jul 03, 2014 2:16 pm

Re: Repository finds element, user code does not

Post by Bat » Thu Jul 03, 2014 2:29 pm

Let's see if I can't help.

Code: Select all

Ranorex.Unknown day = repo.CMTCMT.ExhibitionStartDay.FindSingle(repo.CMTCMT.ExhibitionStartDay.GetPath().ToString() + "/a[@innertext='10']");
day.Click();
Give that a try, I feel like it wouldn't pull from my repo when I did a FindSingle. I could be wrong and would gladly have someone with more knowledge correct this.

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Repository finds element, user code does not

Post by krstcs » Thu Jul 03, 2014 2:40 pm

It appears that you are trying to find part of the element's path as a child, which won't work.

I would suggest that you need to re-structure your repository a bit and make the day element be separate from the container.

I would create a new Rooted folder for the 'ExhibitionStart-calendar' object and then put all of the sub elements under that.

--ExhibitionStartCalendar -> "//div[#'ExhibitionStart-calendar']"
----StartDay -> "//a[@InnerText=$startDayValue]"

This will variablize the day and allow you to pass in a day at run-time so you won't have to do a search for it in user-code.

Your module would then not need user-code and you could just do a "Mouse->Click->Left->StartDay" action. Bind the "startDayValue" variable to data and you are set.


You could also change it just a bit and make either calendar useable with the same module by changing the calendar path to something like "//div[@id~'Exhibition[Start|End]-calendar']" but this might be problematic since they are both displayed. To fix that, you could variablize the "Start|End" part like this "//div[@id~'Exhibition' + $startEndVar + '-calendar']". Then you just need to set the value of the startEndVar variable through a data binding at run-time.
Shortcuts usually aren't...

Sean P
Posts: 5
Joined: Thu Jul 03, 2014 12:03 am
Location: Brea, Ca

Re: Repository finds element, user code does not

Post by Sean P » Thu Jul 03, 2014 5:17 pm

So the problem is that all this is data driven, and its driven by a string that is of the format DateTime (requirement). So when executing the test, a string in the form of a DateTime is passed in, casted to type DateTime then parts of the date time including Month and Year are used to move the calendar to the correct area, also allowing for a leave blank check box which has other functionality with the date picker. So the DateTime is already variable and dynamic at run time. Thats not the problem.

The problem is that the exact same path that works in the repository does not work with the call I made. From what I understand, calling

Code: Select all

repo.CMTCMT.ExhibitionStartDay.FindSingle(string)


will look for elements within the object ExhibitionStartDay and return a Ranorex object, which is what I am looking for. The day that I want to find is an "a tag" within that div with that @innertext.

I dont think the repository is an issue at this point for a few reasons. First, the exact same repository with my writing the entire string out for the XPath works. But when I do a find single on the EXACT SAME parent div, and just fill in the rest in code, Ranorex cant find the elements. I would not expect them to act differently. Also like i said in my original post, I have tried many different XPath structures that work in the repo and not in code. My example is just what was the last thing I tried.

Is there any other insight as to what I can try in code to dynamically find this item?

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Repository finds element, user code does not

Post by Support Team » Thu Jul 03, 2014 8:27 pm

Sean P wrote: Now when I remove the a tag part of the path and in user code do the following:

Code: Select all

Ranorex.Unknown day = repo.CMTCMT.ExhibitionStartDay.FindSingle("/a[@innertext='10']");
day.Click();
Ranorex cannot find the path.
Just remove the leading slash "/" character or put a dot "." in front and everything should be working fine :D
repo.CMTCMT.ExhibitionStartDay.FindSingle("a[@innertext='10']");
// or
repo.CMTCMT.ExhibitionStartDay.FindSingle("./a[@innertext='10']");
FindSingle performs a relative search unless you instruct it to start from the very root element by preceding the path with a "/". Having a "/" at the front of your RanoreXPath always means "search from Host.Local".
By not using a leading slash "/" or by explicitly using a leading ".", you specify a relative path.

(XPath for web sites or changing directories in the Windows command line or Linux bash works the same. Paths are always relative unless you use a leading slash or backslash.)

Regards,
Alex
Ranorex

Sean P
Posts: 5
Joined: Thu Jul 03, 2014 12:03 am
Location: Brea, Ca

Re: Repository finds element, user code does not

Post by Sean P » Thu Jul 03, 2014 10:38 pm

Alex,

Thanks for the response. I have tried both ways, and in both cases, Ranorex failed to find the element.

What should I do next?

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Repository finds element, user code does not

Post by Support Team » Fri Jul 04, 2014 1:36 pm

Well, the leading slash was obviously not the only problem :?
I just realized that you had some question mark operators before the "/a..." part of the RanoreXPath. I did not see that before, sorry!
Did you copy that part with the questions marks "//?/?/?/" to the code as well or did you leave it in the repository?

If you left it in the repository, then the whole part "//?/?/?/" at the end of the repository item path is pretty useless. Both "//" and "?" specify optional levels in the RanoreXPath (see the user guide section) and consequently don't make any sense or difference if there are not followed by something more specifc that Ranorex can search for.

Just try the following path ("//" should be enough since it already tells Ranorex to look in all sub levels of the tree):
repo.CMTCMT.ExhibitionStartDay.FindSingle(".//a[@innertext='10']");
Regards,
Alex
Ranorex Team

Sean P
Posts: 5
Joined: Thu Jul 03, 2014 12:03 am
Location: Brea, Ca

Re: Repository finds element, user code does not

Post by Sean P » Sun Jul 06, 2014 9:41 pm

Alex,

Awesome. It works.

In terms of the XPath, I was using the XPath that Ranorex generated...one of the many iterations that it generated. It never generated the path consistently so i decided to stick with one. You can actually see the difference in the ExhibitionStartDay element and the ExhibitionEndDay element. Regardless, I appreciate your help and I have solved my issue and cleaned up the repository to removed some of the wildcards and optionals.

-Sean