'Assign a value to a "hidden" input webelement, in SeleniumBasic for Chrome
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.
A page that displays a two week calendar defaults to start at today's date, to enter events on specific subsequent days.
I want to change the default 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.
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 code worked for IE, where "jumpdate" (string) = The date I want to replace the default (current) date:
IE.document.getElementById("_ctl1_currentDate").Value = jumpDate
IE.document.getElementById("aspnetForm").submit
The 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. I think 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 argument with the first line of the old IE code.
I cannot get the syntax to do the equivalent of this in Selenium Basic with the .executeScript function.
Solution 1:[1]
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 & "');"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | QHarr |