unable to click option within select tag

Experiences, small talk, and other automation gossip.
bhagya
Posts: 11
Joined: Wed Jan 18, 2012 6:48 am

unable to click option within select tag

Post by bhagya » Wed May 23, 2012 10:00 am

hi, currently i am having a combo box which is identified as SelectTag in ranorex.
I need to click option within that combo box.
Code which i have written:
Ranorex.SelectTag searchCombo = ecmMainPage.FindSingle("/dom[@domain='" + Constants.SERVER_PORT + "']//select[@id='select_savedsearches']");
searchCombo.Focus();
searchCombo.Click();
Ranorex.OptionTag savedSearch = searchCombo.FindSingle("/dom[@domain='" + Constants.SERVER_PORT + "']//select/optgroup[@label='LocalSearch']/option[@InnerText='mysearch']");
savedSearch.Focus();
savedSearch.Click();
Please let me know.

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

Re: unable to click option within select tag

Post by Support Team » Wed May 23, 2012 10:58 am

Hi,

please have a look at follwing forum post:
http://www.ranorex.com/forum/how-to-sel ... -t998.html

Basically the option tag can be selectd using follwoing code:

Code: Select all

savedSearch.Selected = true;
instead of

Code: Select all

savedSearch.Focus();
savedSearch.Click();
Regards,
Tobias
Ranorex Team
.
Image

bhagya
Posts: 11
Joined: Wed Jan 18, 2012 6:48 am

Re: unable to click option within select tag

Post by bhagya » Wed May 23, 2012 1:01 pm

savedSearch.Selected = true;

Above code doesn't fire any event. I want page to be refreshed after selecting the particular value.
The code is written on clicking of option.

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

Re: unable to click option within select tag

Post by Support Team » Thu May 24, 2012 11:00 am

Hi,

Is it a JavaScript method which will be executed after a click on the optionTag?
If yes you can execute it in code via the WebDocument:
yourOptionTag.Selected = true;
yourWebDocument.Self.ExecuteScript("yourJavaScriptMethod()");
Regards,
Markus
Ranorex Support Team
.
Image

bhagya
Posts: 11
Joined: Wed Jan 18, 2012 6:48 am

Re: unable to click option within select tag

Post by bhagya » Thu May 24, 2012 12:49 pm

I want to click the option within combo box. The above solutions are not working. Anyone is having other solution.

sham526
Posts: 34
Joined: Wed Jul 07, 2010 7:12 am
Location: Hyderabad(INDIA)

Re: unable to click option within select tag

Post by sham526 » Fri May 25, 2012 6:02 am

U can Try the below code(VB.NET).

Dim optag_office As System.Collections.Generic.IList(Of OptionTag) = searchCombo .Find(Of Ranorex.OptionTag)("./option")

For c As Integer = 1 To optag_office.Count

Dim opt_office_rxpath As New RxPath("./option[" + c.ToString + "]")
Dim office_opt_port As Ranorex.OptionTag = object_path.FindSingle(Of Ranorex.OptionTag)(opt_office_rxpath)

If office_opt_port.Innertext.ToLower.ToString = "mysearch" Then

object_path.EnsureVisible()
object_path.Click()
optag_office(c - 1).Selected = True
optag_office(c - 1).PerformClick()
object_path.Click()
Delay.Milliseconds(5000)
Exit For

End If

Next

bhagya
Posts: 11
Joined: Wed Jan 18, 2012 6:48 am

Re: unable to click option within select tag

Post by bhagya » Tue Jul 24, 2012 10:04 am

hi All,

Thanks all for your solutions. However, I am still stucked with this problem.
It identifies the option tag within combo box but doent click it. It goes somewhere else and try to click it.
Please let me know.

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

Re: unable to click option within select tag

Post by Support Team » Tue Jul 24, 2012 12:54 pm

Hi,

Where do you want to run your test? Firefox, IE, ...?
Depending on your browser it's not always possible to perform a click. Not because of Ranorex but because every browser has a different structure and a different treatment of elements.

So the better and browser-independent way will be a solution using optionTag.Selected = true; and not optionTag.Click();

If you still convinced that clicking is the way you want to select your optionTag a snapshot of the form including the selectionTag would be very helpful for further guidance.

Regards,
Larissa
Ranorex Support Team
.
Image

bhagya
Posts: 11
Joined: Wed Jan 18, 2012 6:48 am

Re: unable to click option within select tag

Post by bhagya » Tue Jul 24, 2012 1:14 pm

I want to use it in both IE and firefox. I can use option.selected = true, but can you please let me know how the change event will be fired. Because only with selected = true, it doesn't refresh the page. So finding another solution for the problem. Along with this, I have tried 'ExecuteScript' but it is still not working.

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

Re: unable to click option within select tag

Post by Support Team » Tue Jul 24, 2012 2:06 pm

Hi,

Please have a look at the following post (containing of 2 pages):
http://www.ranorex.com/forum/select-con ... t3596.html

Which event should be fired when OptionTag is clicked? Is there a onchange event? What should happen?

Regards,
Larissa
Ranorex Support Team
.
Image

paulgabion
Posts: 5
Joined: Fri Jul 12, 2013 9:24 pm

Re: unable to click option within select tag

Post by paulgabion » Mon Apr 21, 2014 8:42 pm

Hello Support Team,

Good day!

I also encounter the same problem too, in Ranorex 5.0. I need to click on an item in the list box to execute something within the page.

When I was using Ranorex 4.0, this code snippet worked:
SelectTag selTag;
OptionTag opTag;
selTag.TryFindSingle<OptionTag>(".//option[@innertext='" + strValue.Trim() + "']", 120000, out opTag);

opTag.Selected = true;
OptionTag opTagTemp = opTag.PreviousSibling.As<OptionTag>();
opTagTemp.Selected = true;
opTag.Click();
I tried using breakpoints to see if there is a value found in my opTag variable, and it had. However, when
opTag.Click();
is executed, in Ranorex 5.0, it just goes to Location 0,0 (upper-left corner of the page).

Can you help me on this? Thanks!

Best Regards,
Paul
Last edited by paulgabion on Wed Apr 23, 2014 3:18 pm, edited 1 time in total.

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

Re: unable to click option within select tag

Post by Support Team » Wed Apr 23, 2014 1:48 pm

Hello Paul,

May I ask you to answer the following questions?
Is the select tag element expanded when the click is executed?
Do you get any error messages?
Why do you select another option tag as the one where you click on?
Have you already tried using the keyboard to select respectively to fire the event?
Which event should be fired when the option tag is clicked?
Is there an onchange event? What should happen?

Thanks,
Markus
.
Image

paulgabion
Posts: 5
Joined: Fri Jul 12, 2013 9:24 pm

Re: unable to click option within select tag

Post by paulgabion » Wed Apr 23, 2014 3:12 pm

Hello Support Team,

Good day.

Here are my answers:
Support Team wrote: Is the select tag element expanded when the click is executed?
Why do you select another option tag as the one where you click on?
Umm I might not have clarified exactly, but the select tag here is a listbox type. Hence, you need to scroll down to see all the values.
Support Team wrote: Do you get any error messages?
Nope.
Support Team wrote: Have you already tried using the keyboard to select respectively to fire the event?
What do you mean "using the keyboard to select respectively to fire the event"?
Support Team wrote: Which event should be fired when the option tag is clicked?
Is there an onchange event? What should happen?
Yes. There's an onchange event where the values in another control (ex. tree) will change when the option is selected.

Best Regards,
Paul

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

Re: unable to click option within select tag

Post by Support Team » Fri Apr 25, 2014 3:32 pm

Hi Paul,

Depending on your JS code the event might also be thrown when pressing the RETURN key.

It is also possible to raise the event in code.
You can for instance use the following code:
public void selectOptionTag(){
            var selectTag=repo.YourSelectTag;// EDIT: use the specific select tag
            // select an option tag
            OptionTag oTag=selectTag.FindSingle<OptionTag>("./option[@innertext='TheInnertextOfAnOptionTag']");// EDIT: add the specific innertext
            oTag.Selected=true;
            RaiseSelectValueChanged(option tag, "change"); // you can also use another Event
}

public static void RaiseSelectValueChangedEvent(Element element, string eventName)
        {
            var tag = element;
            bool usedTempId = false;
            try
            {
                var selectTag = GetAncestorSelectTag(element);

                var id = selectTag["id"] as string;
                if (string.IsNullOrEmpty(id))
                {
                    id = "_rxtmp" + new Random().Next(int.MaxValue);
                    selectTag["id"] = id;
                    usedTempId = true;
                }

                var script = @"var _rxtag = document.getElementById('" + id + @"');
                                   window.setTimeout(function(){
                                   var ev = document.createEvent('UIEvents'); " +
                                   (usedTempId ? "_rxtag.removeAttribute('id'); " : "") +
                                   @"ev.initUIEvent('" + eventName + @"', true, true, window, 1);
                                   _rxtag.dispatchEvent(ev);}, 1);";

                var host = GetScriptHost(tag);
                host.InvokeAction("executescript", script);
            }
            catch (Exception ex)
            {
                throw new RanorexException("Failed to raise event '" + eventName + "'. ", ex);
            }
        }

        static Element GetAncestorSelectTag(Element element)
        {
            var current = element.Parent;
            while (current != null)
            {
                if (("" + current["tagname"]) == "select")
                    return current;
                current = current.Parent;
            }
            return null;
        }

        static Element GetScriptHost(Element element)
        {
            var current = element.Parent;
            while (current != null)
            {
                foreach (var act in current.Actions)
                    if (act.Name == "executescript")
                        return current;
                current = current.Parent;
            }
            return null;
        }
Before you call “RaiseSelectValueChanged” you need to first select the option tag by setting its optionTag.Selected = true property to true.

Regards,
Markus
.
Image

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

Re: unable to click option within select tag

Post by krstcs » Fri Apr 25, 2014 4:03 pm

I have found that using the keyboard (Up, Down, Enter) is the best way to do it. It works in every browser and fires the correct events. PerformClick (or any other invoke method) is not guaranteed to fire the events associated with the element.

If you use the following code, it works every time. It will look at the selecttag element and see what is selected and what you want to be selected and move the correct direction until the desired optiontag is selected, and then press the Enter key.

Code: Select all

public static void SelectOption(SelectTag selectTag, OptionTag optionTag, Duration dur) {
	List<string> options = GetOptionsText(selectTag);

	int desiredIndex = options.IndexOf(optionTag.TagValue);

	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 != null ? ot.TagValue : "");
	}

	return options;
}


private static void DoSelectAction(SelectTag selectTag, int desiredIndex, List<string> options, Duration dur) {
	int selectedIndex = 0;

	if (selectTag.TagValue != null) {
		List<OptionTag> optiontags = new List<OptionTag>(selectTag.Find<OptionTag>(".//optiontag[@TagValue='" + selectTag.TagValue + "']"));

		if (options.Count > 0) {
			OptionTag selectedOptionTag = optiontags[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;
}
Shortcuts usually aren't...