Currently, I'm utilizing VBA Selenium Basic to parse through an HTML document in attempt to modify the attribute style
within a td
element. Despite my efforts, I have been unsuccessful in finding methods such as webelement.RemoveAtrr
, webelement.SetAtrr
, or similar alternatives.
After conducting extensive research on DuckDuckGo and Stack Overflow, it seems that Selenium Basic only allows for reading attributes, not altering or removing them directly. As a result, I turned to using JavaScript for this task.
Upon accessing the browser console, I executed the following lines of code:
var cell = document.querySelector('body > div > form:nth-child(5) > table > tbody > tr:nth-child(1) > td:nth-child(1)');
cell.removeAttribute('class');
cell.setAttribute('style','background-color: #00FF7F;';
The JavaScript successfully updated the page by discarding old class information and applying new style information. Encouraged by this success, I attempted to replicate the process in VBA with the following script:
oChrome.ExecuteScript "var sfCell = document.querySelector('body > div > form:nth-child(5) > table > tbody > tr:nth-child(1) > td:nth-child(1)');"
oChrome.ExecuteScript "sfCell.removeAttribute('class');"
oChrome.ExecuteScript "sfCell.setAttribute('style','background-color: #00FF7F;';"
Unfortunately, this approach did not yield the desired results. While the first line executes without errors, it fails to create the variable (as confirmed in the Console window). The second line triggers a JavaScript error instead of a Visual Basic one, stating:
sfCel.removeAttribute is not a function
.
If anyone has insights on how to execute multiline JavaScript instructions using ExecuteScript or alternative methods to remove and alter attributes without relying on JavaScript, any help would be greatly appreciated!
Thank you!