1

HTML that I want to manipulate using JavascriptExecutor doesn't have id. It only contains name, class and tag. Out of these only name is unique and rest two are common for many other WebElements present in DOM.

I tried:

String javaScriptCode = "document.getElementsByName('ac118672').setAttribute('value','00031454476543');";

jse().executeScript(javaScriptCode);

this gave me error document.getElementsByName(...).setAttribute is not a function which is obvious because document.getElementsByName give collection of all elements as described here https://www.w3schools.com/jsref/met_doc_getelementsbyname.asp.

So is there any other way I can change value of value attribute using JavascriptExecutor?

References I took:

How to edit HTML (remove readonly) and type in input box using JS Executor?

JavaScriptexecutor setAttribute value on selenium

paul
  • 4,333
  • 16
  • 71
  • 144

2 Answers2

1

After trying many ways, sleeps before and after, it wasn't working.

Finally what worked for me is:

document.getElementsByName('ac118672')[0].value='00031454476543';

i.e. jse().executeScript(document.getElementsByName('ac118672')[0].value='00031454476543');

I guess setAttribute also does same thing, but it didn't work.

I tested it on both Chrome and Gecko Driver latest versions, on Windows 7.

paul
  • 4,333
  • 16
  • 71
  • 144
  • 1
    here is the difference between setAttribute and direct set value https://stackoverflow.com/questions/22151560/what-is-happening-behind-setattribute-vs-attribute – Vladimir Efimov Nov 29 '18 at 09:41
0

getElementsByName returns an array of element , in your case if you have just one element with the name 'ac118672' you should access it with

document.getElementsByName('ac118672')[0].setAttribute

that should work

paul
  • 4,333
  • 16
  • 71
  • 144
Mehdi Bahra
  • 319
  • 1
  • 8