Home > database >  Assign a value to a "hidden" input webelement, works in VBA for IE but not SeleniumBasic f
Assign a value to a "hidden" input webelement, works in VBA for IE but not SeleniumBasic f

Time:01-21

I have to migrate a web automation program from IE controlled by Excel VBA to Chrome using selenium basic in Excel VBA. The website itself is not public so I cannot share a link. The aspect I am having trouble with is a page that displays a 2 week calendar defaulting to start at today's date, in order to enter events on specific subsequent days. I want to change the defualt date to another date and have the website alter the dates displayed to start on that new date. This involves changing the "Value" attribute of a hidden input webelement whose ID = "_ctl1_currentDate" which worked with IE using the native driver in VBA excel but I cannot get this to work with the Selenium Basic driver for Excel VBA for chrome.

This is the html of the webelement:

<input name="_ctl1:currentDate" type="hidden" id="_ctl1_currentDate" value="19/01/2022">

This is the Javascript associated with this webelement:

<script language="javascript">

    /* une fonction permettant de se déplacer dans calendrier */
    function move(direction) {
        var date = document.getElementById("_ctl1_currentDate").value;
        var day;
        var month;
        var year;
        var t = new Date();
        var newDate;
        var newMonth;
        var newDay;

        if (date.charAt(2) == "/") {
            day = date.substring(0, 2);
            if (date.charAt(5) == "/") {
                month = date.substring(3, 5);
                year = date.substring(6, 10);
            } else {
                month = date.substring(3, 4);
                year = date.substring(5, 9);
            }
        } else {
            day = date.substring(0, 1);
            if (date.charAt(4) == "/") {
                month = date.substring(2, 4);
                year = date.substring(5, 9);
            } else {
                month = date.substring(2, 3);
                year = date.substring(4, 8);
            }
    }
    t.setDate(1);   
        t.setYear(year);
        t.setMonth(month - 1);
        t.setDate(day); 
        t = DateAdd("d", t, direction * 10);
        newMonth = t.getMonth()   1;
        newDay = t.getDate();
        if (newDay < 10) { newDay = "0"   newDay; }
        if (newMonth < 10) { newMonth = "0"   newMonth; }
        newDate = newDay   "/"   newMonth   "/"   t.getFullYear();
        document.getElementById("_ctl1_currentDate").value = newDate;
    document.getElementById("aspnetForm").submit(); 
    }

    /* cette fonction fait un submit de la form globale TheForm, présente sur chaque page de portail */
    function redirectToQstWithDate(typeQST, OptMA, date) {

        var theForm = getTheForm();
        theForm.action = ''   document.getElementById("_ctl1_redirectUrl").value   '';
        if (OptMA && OptMA != "") {
          document.getElementById('OPTMA').value = OptMA;
        }
        document.getElementById('QST').value = typeQST;
        document.getElementById('ScreenSize').value = window.screen.width;
        document.getElementById('InputDate').value = date;

        __doPostBack('', '');
    }
</script>

This is the code that worked for IE previously, where "jumpdate" (string) = The new date I want to replace the default (current) date:

IE.document.getElementById("_ctl1_currentDate").Value = jumpDate
IE.document.getElementById("aspnetForm").submit

This is the new attempt with the equivalent of the first line of the above code in selenium basic, which produces the SeleniumError "Element not interactable". "ch" is the Chrome driver reference:

 ch.FindElementById("_ctl1_currentDate").SendKeys (jumpDate)

I have the feeling that Selenium will not allow me to change the value of this element in chrome despite being able to do it for IE without Selenium. I think that instead I need to fire the assocated Javascript; which is essentially what I was doing with the second line of the old IE code, whilst changing the arguement with the first line of the old IE code. Unfortunately I cannot get the correct syntax to do the equivalent of this is in Selenium Basic with the .executeScript function. Any advice for the correct VBA code with the selenium driver would be appreciated!

CodePudding user response:

The translation for selenium basic using ExecuteScript method to set the value attribute would be as follows, where you concatenate in the jumpDate value

.ExecuteScript "document.getElementById('_ctl1_currentDate').setAttribute('value','" & jumpDate & "');"
  •  Tags:  
  • Related