unable to click option within select tag
unable to click option within select tag
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.
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.
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: unable to click option within select tag
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:
instead of
Regards,
Tobias
Ranorex Team
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;
Code: Select all
savedSearch.Focus();
savedSearch.Click();
Tobias
Ranorex Team
Re: unable to click option within select tag
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.
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.
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: unable to click option within select tag
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:
Markus
Ranorex Support Team
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
Re: unable to click option within select tag
I want to click the option within combo box. The above solutions are not working. Anyone is having other solution.
Re: unable to click option within select tag
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
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
Re: unable to click option within select tag
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.
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.
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: unable to click option within select tag
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
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
Re: unable to click option within select tag
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.
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: unable to click option within select tag
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
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
-
- Posts: 5
- Joined: Fri Jul 12, 2013 9:24 pm
Re: unable to click option within select tag
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:
Can you help me on this? Thanks!
Best Regards,
Paul
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.
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: unable to click option within select tag
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
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
-
- Posts: 5
- Joined: Fri Jul 12, 2013 9:24 pm
Re: unable to click option within select tag
Hello Support Team,
Good day.
Here are my answers:
Best Regards,
Paul
Good day.
Here are my answers:
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: 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?
Nope.Support Team wrote: Do you get any error messages?
What do you mean "using the keyboard to select respectively to fire the event"?Support Team wrote: Have you already tried using the keyboard to select respectively to fire the event?
Yes. There's an onchange event where the values in another control (ex. tree) will change when the option is selected.Support Team wrote: Which event should be fired when the option tag is clicked?
Is there an onchange event? What should happen?
Best Regards,
Paul
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: unable to click option within select tag
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:
Regards,
Markus
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
Re: unable to click option within select tag
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.
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...