How to find, replace and write text in to the text field by xpath-javascript?
Clash Royale CLAN TAG#URR8PPP
How to find, replace and write text in to the text field by xpath-javascript?
Text-field with elements
HTML
This is my first question here. Any suggestions to improve are helpful.
I have added two images. First image shows that text-field/box along with HTML elements. I need to get the value "www.google.com/abc
" and replace abc
with xyz
and write back to the same text-box.
www.google.com/abc
abc
xyz
1) I am able to do this, when element has unique
attributes like ID, ClassName
as below
unique
like ID, ClassName
var tmp1 = (document.getElementById('properties.landingPageUrl').value.toLowerCase()).replace('abc','xyz');
document.getElementById("properties.landingPageUrl").value=tmp1;
2) In the attached first image,<input type="text" autocomplete="off" class="a-input-text a-width-extra-large" value="" data-reactid=".0.5.2.0.1.4.1.0.0:$0.1.0.0:$/=10">
has ClassName
a-input-text a-width-extra-large
, which is not identical. For another text box also, it shows same ClassName. So, to make it unique used xpath: //span[contains(text(),"Third party click tracking URL")]/../../../div[2]//input
, which points to a particular text-box (for field 'Third party click tracking URL').
<input type="text" autocomplete="off" class="a-input-text a-width-extra-large" value="" data-reactid=".0.5.2.0.1.4.1.0.0:$0.1.0.0:$/=10">
ClassName
a-input-text a-width-extra-large
xpath: //span[contains(text(),"Third party click tracking URL")]/../../../div[2]//input
How can i get the value from text-box and replace by some pattern and write back to the text-box (`in the second instance`) ?
I tried document.EelementByXpath
, but not working
document.EelementByXpath
ERROR
Uncaught TypeError: document.getElementByXpath is not a function
at HTMLInputElement.eval (userscript.html?id=935b3faf-380a-4fa1-ba80-e2a14b01fe91:9428)
at HTMLInputElement.dispatch (userscript.html?id=935b3faf-380a-4fa1-ba80-e2a14b01fe91:3335)
at HTMLInputElement.eventHandle (userscript.html?id=935b3faf-380a-4fa1-ba80-e2a14b01fe91:2944)
(anonymous) @ userscript.html?id=935b3faf-380a-4fa1-ba80-e2a14b01fe91:9428
dispatch @ userscript.html?id=935b3faf-380a-4fa1-ba80-e2a14b01fe91:3335
eventHandle @ userscript.html?id=935b3faf-380a-4fa1-ba80-e2a14b01fe91:2944
So, is there any method which can be used for xpaths or any other way to achieve the second instance ?
@cale_b Thank you. Can I do the same for xpath ? since document.getElementByXpath doesn't work, should document.evaluate to be used ? I'm not sure.
– Jackie
Aug 10 at 15:41
@Jackie: Help us help you by providing the exact error message as cale_b requested. You should also provide a Minimal, Complete, and Verifiable example. Thank you.
– kjhughes
Aug 10 at 16:02
Why have you still not posted your error message? Also, your "MCVE" is neither complete nor verifiable. Moving on. Good luck.
– kjhughes
Aug 10 at 16:37
The code you have shown cannot give the error message you claim. So, not only is that not an MCVE, it's not coherent either. In addition this is an obvious "XY Problem". Link to the target page and describe what your desired result is (hint: it has nothing to do with XPath).
– Brock Adams
Aug 10 at 21:15
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Welcome! Error messages matter. Copy / paste the exact message into your question, please.
– cale_b
Aug 10 at 15:39