0

I'm using VBA Selenium Basic to parse a HTML document, trying to change the attribute style from a td element. I tried to find methods like webelement.RemoveAtrr, webelement.SetAtrr or the like in vain.

After searching through DuckDuckGo and Stack Overflow, I came to think Selenium Basic can't remove, change or insert an attribute -- only read them. So I tried to do it using javascript.

Then I came to browser console and used the following lines:

var cell = document.querySelector('body > div > form:nth-child(5) > table > tbody > tr:nth-child(1) > tr:nth-child(1)');
cell.removeAttribute('class');
cell.setAttribute('style','background-color: #00FF7F;';

The javascript works fine on the page, discard old class information and adding new style information. So, I came to VBA and tried this:

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;';"

But it didn't work. The first line runs but doesn't create the variable, as I can see in Console window. The second line retrieves an error from javascript, not from VB: sfCel.removeAttribute is not a function.

How should I perform javascript multiline instructions using ExecuteScript? I also accept ways to remove and change attributes not using javascript, but I'm honestly curious about the problem in the above code.

Thanks!

César Rodriguez
  • 296
  • 5
  • 16
  • `"var sfCell = ""document.query...."""` here you're creating a *string* variable holding a piece of javascript. Try removing the embedded double quotes. – Tim Williams Jan 26 '21 at 18:03
  • Thanks for your answer. As a matter of fact, it was wrong to insert the quotes, either in console or in VBA, but it was not the problem. I have edited my question to remove the quotes. – César Rodriguez Jan 26 '21 at 21:35
  • Seems like your query selector is not finding the element you want: perhaps there's a timing issue? If the element is dynamic it may not yet be rendered when the VBA runs to execute the js. – Tim Williams Jan 26 '21 at 22:05
  • It's not also a timing issue. I added a stop point and run it only when everything was loaded. Then I ran the line ```var sfCell = ...``` (it returns no error). Then I go to the Chrome window console and type sfCell, but there is no javascript variable named sfCell. It is blowing my mind! By the way, I haven't find any page teaching how to run multiline javascrpit commands. – César Rodriguez Jan 26 '21 at 23:00
  • 1
    According to https://ui.vision/rpa/docs/selenium-ide/executescript "The script fragment will be executed as the body of an anonymous function", so perhaps your `sfCell` isn't making it to the global scope. Try `window.sfCell = ...` to attach it to the global (window) scope. Or alternatively omit the `var` and that should put the variable in the global scope - see https://stackoverflow.com/questions/2485423/is-using-var-to-declare-variables-optional – Tim Williams Jan 26 '21 at 23:24
  • 1
    And previously: https://stackoverflow.com/questions/13858096/the-method-executescriptselenium-web-driver-cannot-define-a-global-variable-fo – Tim Williams Jan 26 '21 at 23:30
  • YOU ARE THE GUY! It's all about scope. Feel free to answer the question, so I can mark it as the right one. Thank you very much! – César Rodriguez Jan 27 '21 at 01:21

1 Answers1

1

According to https://ui.vision/rpa/docs/selenium-ide/executescript

"The script fragment will be executed as the body of an anonymous function"

So perhaps your sfCell isn't making it to the global scope.

Try window.sfCell = ... to attach it to the global (window) scope.

Or alternatively omit the var and that should put the variable in the global scope - see Is using 'var' to declare variables optional?

Tim Williams
  • 154,628
  • 8
  • 97
  • 125