Forum Discussion

vthomeschoolmom's avatar
vthomeschoolmom
Super Contributor
5 years ago

New to QuerySelector actions .. selecting an item from a <select> tag

Consider this monstrosity:

 

function DateFromPicker()
{
  var month = "Apr";
  var year = "2020";
    
  var cssSelector = "#ui-datepicker-div > div > div > select.ui-datepicker-month";
  var selMonth = Aliases.browser.pageMain.panDatePicker.QuerySelector(cssSelector);
  selMonth.Click();
  ClickMonthOnPicker(selMonth, month);
    
  cssSelector = "#ui-datepicker-div > div > div > select.ui-datepicker-year";
  
      
  
}

// this just holds the long (ugly) switch case so the calling method is not so long
function ClickMonthOnPicker(selMonth, month)
{
  switch (month)
  {
    case "Jan":
      selMonth.selectedIndex = 0;
      break;
    case "Feb":
      selMonth.selectedIndex = 1;
      break;
    case "Mar":
      selMonth.selectedIndex = 2;
      break;
    case "Apr":
      selMonth.selectedIndex = 3;
      break;
    case "May":
      selMonth.selectedIndex = 4;
      break;
    case "Jun":
      selMonth.selectedIndex = 5;
      break;
    case "Jul":
      selMonth.selectedIndex = 6;
      break;
    case "Aug":
      selMonth.selectedIndex = 7;
      break;
    case "Sep":
      selMonth.selectedIndex = 8;
      break;
    case "Oct":
      selMonth.selectedIndex = 9;
      break;
    case "Nov":
      selMonth.selectedIndex = 10;
      break;
    case "Dec":
      selMonth.selectedIndex = 11;
      break;

    default:
      Log.Error("Error in test. Valid month string needed for date picker.");
      break;
  }
}

Is there something besides selectedIndex ... a relative that would allow me to select by the container text? For example select

 

<option value="0">Jan</option>

 

By "Jan"? I am still looking at what kind of "thing" selMonth is. Thanks for your patience.

  • Hi,

     

    Do you need to control date picker popup or just enter some date into the field and proceed?

    If the latter and value enter via .Keys() method call does not work, the following worked for me in (almost?) all cases:

    -- Assign required date using correct format to the field via either direct assignment to the .Value/wText/etc. property or via the .SetText() call. Investigate what will work in your case;

    -- Open date picker window. If you are lucky, it will be opened automatically after you either assign the value to the property or perform .SetFocus()/.Focus() call. If not, try to use either some key press (Down Arrow, for example) or click on the 'calendar' icon. The key point here is that date picker popup should be opened with correct date already selected;

    -- Confirm date selection by pressing Enter key. Quite often it works even if you do dateField.Keys('[Enter]') without the necessity of something like datePickerPopup.Keys('[Enter]').

     

    Does this help?

     

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Do you need to control date picker popup or just enter some date into the field and proceed?

    If the latter and value enter via .Keys() method call does not work, the following worked for me in (almost?) all cases:

    -- Assign required date using correct format to the field via either direct assignment to the .Value/wText/etc. property or via the .SetText() call. Investigate what will work in your case;

    -- Open date picker window. If you are lucky, it will be opened automatically after you either assign the value to the property or perform .SetFocus()/.Focus() call. If not, try to use either some key press (Down Arrow, for example) or click on the 'calendar' icon. The key point here is that date picker popup should be opened with correct date already selected;

    -- Confirm date selection by pressing Enter key. Quite often it works even if you do dateField.Keys('[Enter]') without the necessity of something like datePickerPopup.Keys('[Enter]').

     

    Does this help?