The web page https://www7.skatteverket.se/portal/rakna-ut-skatt (by the Swedish tax authority and in Swedish) is used to figure out how much Swedish tax to pay.
For your info, the web page seems to be constructed using some Vue framework and also contains a lot of nested shadow roots that can be seen in the DOM, using the browsers developer tool and looking in the Element tab.
To figure out how much tax to pay, you need to input a lot of background info into input fields. These input field could be radio buttons or combo boxes, drop-down inputs or plan text inputs, normally in the form of digits. Also other buttons like ’next’ buttons or ’expand’ buttons to expose more input fields are quite common.
The web page is designed to manually input this background info by a person.
But instead I want to input all the background information programmatically from info in an excel sheet, using technics like VBA, JavaScript and Selenium Basic.
For the most part this already works very well. But I have a specific problem when programmatically entering text (digits) into a plain input field.
The problem occur when the current section of the web page contains such a text input fields and after text is input in it, and all other kind of fields, one should hit a ’next’ button to get to the next section of the web page, with normally more different kind of input fields.
It works using VBA, JavaScript and Selenium to input the text (digits) into the plain input field and the programmatically inputted value is shown on the web page.
But when hitting the ’next’ button to come to the web page’s next section, the text inputted into the plain input field disappear and an error message is shown telling you that you need to input value into the input field.
This error only occur for plain input fields and not for drop-downs or combo boxes.
If you in this situation manually input the same value into the input field, as previously has been programmatically inputted and displayed, the error disappears and the next section of the web page is displayed when you also hit the ’next’ button.
So the internal code in the web page somehow makes a difference when the same value is inputted into a plain input field manually or programmatically.
I have guessed, but I’m not sure, that this problem can be because different event listeners are triggered in the two situations. Therefore I have investigated using the browsers developer tool what event listeners are present on the input fields and also on its ancestors.
The input field where the problem is has three standard event listeners with the event type focus, change and blur.
To understand and log what event listeners are triggered and in what order, I have overridden the original dispatchEvent method, using the JavaScript code below.
// Save the original dispatchEvent method
const originalDispatchEvent = EventTarget.prototype.dispatchEvent;
// Override dispatchEvent
EventTarget.prototype.dispatchEvent = function (event) {
// Log which event type was fired
console.log(`Event type fired: ${event.type}`);
// Log the element that triggered the event
console.log("Event target:", event.target);
// Log the dispatched event
console.log("Event dispatched:", event);
// If the event has a 'detail' property, log it
if (event.detail) {
console.log("Event detail:", event.detail);
}
// Call the original dispatchEvent method
return originalDispatchEvent.call(this, event);
};
Focusing on the input field in question when entering values (digits) into it manually only the change event is triggered.
When the same input field was programmatically filled with digits the following event listernas was dispatched: focus, input, change and blur, using the following code:
actionNode.focus(); // Step 1: Focus
actionNode.value = actionCont; // Step 2: Set
actionNode.dispatchEvent(new Event('input', { bubbles: true })); // Step 3: Trigger input
actionNode.dispatchEvent(new Event('change', { bubbles: true })); // Step 4: Trigger change
actionNode.blur(); // Step 5: Remove focus
Nothing of this made the problem go away. The digits that is programmatically inserted into the input field and displayed on the web page, still disappear when the ’next’ button is hit.
I need some serious help to fix this situation.