Page 1 of 1

Cant click on elemnt in dropdown

Posted: Wed Jan 29, 2014 1:47 pm
by rholdberh
Hi,
i am using Ranorex 4.1.4 with chrome and i cant click on element in dropdown.

I have a dropdow with about 30 items, but only 10 of them are visible, so u need to scroll other items.
Clicking on a visible elemet works for me, but on not visible (thats one needed to scrolled) does not works.

Xpath position of my click :

//body//div[#'addDocument']/ul//litag/a[@innertext='Rental Invoice']

So maybe u know any solution how to choose elemet from this dropdown?
Regards

Roman

Re: Cant click on elemnt in dropdown

Posted: Wed Jan 29, 2014 1:54 pm
by rholdberh
ADD: i try "Invoke Action" with perfom click and it works. But could u explain me what Invoke action is rly doing?

Re: Cant click on elemnt in dropdown

Posted: Wed Jan 29, 2014 3:07 pm
by krstcs
Invoke Action actually fires the event specified by the default handler on the object in question. It is very handy/powerful, but also has some hidden problems in that if your developers didn't code the events just right they won't fire correctly when you use the Invoke Action on the item.


The problem you are having is that Chrome/Firefox/IE all handle dropdown lists differently and Chrome and Firefox do not report the location of the dropdown list (among other things) to the UI, so Ranorex cannot find it (this is a simplistic explanation, there is more to it in reality...).

I would suggest that you look at using keyboard actions on the list of elements instead of mouse clicks.

If your list always starts with the first ten elements visible (for example), then just use a keyboard {Down} key action until you find the element you are looking for and then do a keyboard {Enter} action.

I use the following class. Note that I have it set to ALWAYS use the keyboard (Chrome) version, since I have found it to be the only reliable method...

Code: Select all

	public static class Select
	{
        public static WebDocument GetRootDOM(Element thisElement) {
        	if (thisElement.PreferredCapability.DisplayName.ToLower().Equals("webdocument")) {
        		return thisElement;
        	}
        	
        	return GetRootDOM(thisElement.Parent);
        }
        
		public static void SelectOption(SelectTag selectTag, int desiredIndex, Duration dur) {
			SelectOption_CHROME(selectTag, desiredIndex, dur);
		}
        public static void SelectOption(SelectTag selectTag, OptionTag optionTag, Duration dur) {
			SelectOption_CHROME(selectTag, optionTag, dur);
//        	string browser = GetRootDOM(selectTag).BrowserName;
//        	string browserVer = GetRootDOM(selectTag).BrowserVersion;
//        	
//        	switch (browser) {
//        		case "Chrome":
//        			SelectOption_CHROME(selectTag, optionTag, dur);
//        			break;
//        		case "IE":
//        			if (browserVer.Contains("8.0.")) {
//        				SelectOption_CHROME(selectTag, optionTag, dur);
//        			} else {
//        				SelectOption_Normal(optionTag);
//        			}
//        			break;
//        		default:
//        			SelectOption_Normal(optionTag);
//        			break;
//        	}
        }
        private static void SelectOption_Normal(OptionTag optionTag) {
        	Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item '" + optionTag.GetPath().ToResolvedString() + "' at Center.");
        	optionTag.Click();
        }
        private static void SelectOption_CHROME(SelectTag selectTag, OptionTag optionTag, Duration dur) {
			List<string> options = GetOptionsText(selectTag);
        	
        	int desiredIndex = options.IndexOf(optionTag.TagValue);
        	
        	DoSelectAction(selectTag, desiredIndex, options, dur);
        }
        private static void SelectOption_CHROME(SelectTag selectTag, int desiredIndex, Duration dur) {
			List<string> options = GetOptionsText(selectTag);
        	
        	DoSelectAction(selectTag, desiredIndex, options, dur);
        }
		
		private static List<string> GetOptionsText(SelectTag selectTag) {
        	List<string> options = new List<string>();
        	
        	foreach (OptionTag ot in selectTag.Find<OptionTag>(".//option")) {
        		options.Add(ot.TagValue);
        	}
        	
        	return options;
		}
		
		private static void DoSelectAction(SelectTag selectTag, int desiredIndex, List<string> options, Duration dur) {
        	int selectedIndex = 0;
        	
        	string selectedTagValue = "";
        	
        	if (selectTag.TagValue != null) {
	        	selectedTagValue = selectTag.TagValue;
	        	
	        	OptionTag selectedOptionTag = selectTag.Find<OptionTag>(".//option[@TagValue='" + selectedTagValue + "']")[0];
	        	
	        	selectedIndex = options.IndexOf(selectedOptionTag.TagValue);
        	}
        	
        	Keyboard.PrepareFocus(selectTag);
        	
        	Duration oldKeyPressTime = Keyboard.DefaultKeyPressTime;
        	Keyboard.DefaultKeyPressTime = 15;
        	Duration keyPressDuration = dur;
        	
        	if (desiredIndex < selectedIndex) {
        		for (int i = desiredIndex; i < selectedIndex; i++) {
        			//up
        			Report.Log(ReportLevel.Info, "Keyboard", "Key 'Up' Press with focus on '" + selectTag.GetPath() + "'.");
        			Keyboard.Press(System.Windows.Forms.Keys.Up, Keyboard.DefaultScanCode, keyPressDuration, 1, true);//Keyboard.DefaultKeyPressTime, 1, true);
        		}
        	} else if (selectedIndex < desiredIndex) {
        		for (int i = selectedIndex; i < desiredIndex; i++) {
        			//down
            		Report.Log(ReportLevel.Info, "Keyboard", "Key 'Down' Press with focus on '" + selectTag.GetPath() + "'.");
         			Keyboard.Press(System.Windows.Forms.Keys.Down, Keyboard.DefaultScanCode, keyPressDuration, 1, true);//Keyboard.DefaultKeyPressTime, 1, true);
       			}
        	}
        	
            Report.Log(ReportLevel.Info, "Keyboard", "Key 'Enter' Press with focus on '" + selectTag.GetPath() + "'.");
        	Keyboard.Press(System.Windows.Forms.Keys.Enter, Keyboard.DefaultScanCode, keyPressDuration, 1, true);//Keyboard.DefaultKeyPressTime, 1, true);
        	
        	Keyboard.DefaultKeyPressTime = oldKeyPressTime;
		}
	}
In your UserCode, just pass in the SelectTag element (element not repo object) and the OptionTag element and a duration (this sets a new duration for the keyboard press time, so you can speed it up or slow it down depending on your needs).

Note that you might need to change the types to match what you are using in your system (LiTag, ATag, etc.).

The call looks like this:

Code: Select all

<Containing Library>.Select.SelectOption(repo.MyDomObject.Body.MySelectTag.Self, repo.MyDomObject.Body.MySelectTag.MyDesiredOptionTag, Program.ShortDuration);

Re: Cant click on elemnt in dropdown

Posted: Fri Jan 31, 2014 10:55 am
by rholdberh
Oh thanks with this dropdown its works.
But in my application i had another one.

i am trying to focus on it with this Xpath:
dom[@domain='....']/body//div[@class='inforSplitter']//div[@class='leftPane']//div[@class='dropDownList entityType']/select[@name='entityType']

and focus doenst works, i cant even Highlight it

Any ideas?

Re: Cant click on elemnt in dropdown

Posted: Fri Jan 31, 2014 2:21 pm
by krstcs
It appears to be greyed out in your screen capture, which means that Spy believes it is not visible for some reason, or that it is no longer valid.

Re: Cant click on elemnt in dropdown

Posted: Fri Mar 14, 2014 11:41 am
by mdgairaud
i'm using a very simple method to get an element inside a dropbox that works in all browser (IE, Chrome and Firefox).

Simply open the combobox and "write" the option you want to click, it's automatically selected. Then click againg the combobox to close the comboo with the selected option or send an {Enter} keypress (depends on browser).

I know that is a very ugly solution and isn't as fancy as krstsc solution, but it saves a lot of time for me (and headaches).

Re: Cant click on elemnt in dropdown

Posted: Mon Mar 17, 2014 2:12 pm
by krstcs
Actually, your solution may be better than mine since it is much simpler (which is what we really want, right??).

However, it does depend heavily on the implementation of the browser's HTML renderer.

The problem is if a browser maker (let's say Google since they seem to be the ones that are doing crazy stuff lately) decides not to implement the search-as-you-type method on the drop-down.

I had also thought about doing it that way, but decided keyboard up/down and enter were safer and more reliable. I had specifically thought about typing the first letter of the phrase until it was highlighted, which gives a similar result. But, again, it might not work for all implementations.

I still might try it as it may be a lot faster than up/down. :D

Re: Cant click on elemnt in dropdown

Posted: Tue Mar 18, 2014 1:52 pm
by mdgairaud
haha, if they decide to not implement it for sure another trick can be done :D