paul
paul

Reputation: 4487

JavascriptExecutor: setAttribute using name

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

Upvotes: 1

Views: 1518

Answers (2)

paul
paul

Reputation: 4487

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.

Upvotes: 1

Mehdi Bahra
Mehdi Bahra

Reputation: 319

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

Upvotes: 0

Related Questions