1

I am using Excel VBA to change content in HTML, I found the following command and it works fine, but when I try to assign the value of a cell in excel it gives an error, please help.

Driver.ExecuteScript ("document.getElementById('tieudeChart').innerHTML = 'textaaa';")

Error: javascripterror: cell1 is not defined

cell1 = Main_AT.Range("ae5").Value
Driver.ExecuteScript ("document.getElementById('tieudeChart').innerHTML= cell1;")
vitamin01
  • 51
  • 7
  • 1
    Maybe: `Driver.ExecuteScript ("document.getElementById('tieudeChart').innerHTML= " & cell1 & ";")` ?? OR maybe: `Driver.ExecuteScript ("document.getElementById('tieudeChart').innerHTML= '" & cell1 & "';")` – braX Sep 10 '22 at 06:20

1 Answers1

1

You want the variable outside of the quotes, and since it's a string, put single quotes around it like this:

Driver.ExecuteScript "document.getElementById('tieudeChart').innerHTML= '" & cell1 & "';"

Note: This assumes cell1 does not have a single quote in it.

This should handle the cell having quotes.

Driver.ExecuteScript "document.getElementById('tieudeChart').innerHTML= '" & Replace(cell1, "'", "''") & "';"
braX
  • 11,506
  • 5
  • 20
  • 33
  • It's very useful. Can you help me this command with document.getelementbyxpath gives error "document.getelementbyxpath is not function"?? – vitamin01 Sep 10 '22 at 10:21
  • no idea if this will help... https://stackoverflow.com/questions/10596417/is-there-a-way-to-get-element-by-xpath-using-javascript-in-selenium-webdriver - but that's a separate question – braX Sep 10 '22 at 11:14