简体   繁体   English

为什么我不能在其中输入文字<input>带有 javascript 代码的字段? 文字闪回原值

[英]Why can't I input text into this <input> field with javascript code? The text flashes back to original value

Watch the video to see what I mean exactly https://youtu.be/kvdVNwTjw5c Thank you!观看视频以了解我的意思https://youtu.be/kvdVNwTjw5c谢谢!

I'm trying to automate login this website shopee.sg/buyer/login I need to input the login name first but I couldn't do it with my codes, it always changed back to original value after the code is executed.我正在尝试自动登录这个网站shopee.sg/buyer/login我需要先输入登录名,但我的代码无法做到这一点,它总是在代码执行后变回原始值。 I have used codes document.getElementsByClassName("_56AraZ")[0].value = "my username";我使用了代码document.getElementsByClassName("_56AraZ")[0].value = "my username"; or document.getElementsByClassName('_56AraZ')[0].setAttribute('value',"my username");document.getElementsByClassName('_56AraZ')[0].setAttribute('value',"my username"); the text in the field appeared on screen to be changed but then the text flashed back to the previous value after I clicked to other field or clicked somewhere else, it didn't really change.该字段中的文本出现在屏幕上要更改,但是在我单击其他字段或单击其他位置后,文本闪回到以前的值,它并没有真正改变。 I know this code can be successfully on other websites but this website is tricky for me?我知道这个代码可以在其他网站上成功,但是这个网站对我来说很棘手? Can anyone help me to input text to this field successfuly?谁能帮我成功地在这个字段中输入文本? Thank you!谢谢!

Probably something else also tries to write in that field - it may be other script or the browser (saved form data).可能还有其他东西也尝试在该字段中写入 - 它可能是其他脚本或浏览器(保存的表单数据)。 I suggest you delayed your script with a setTimeout function for some absurd time (like 2 seconds or more) and if it helps, lower the delay to a working, but acceptable value.我建议您使用 setTimeout function 将脚本延迟一些荒谬的时间(例如 2 秒或更长时间),如果有帮助,请将延迟降低到可以工作但可以接受的值。 so your script is the last thing that writes to that field.所以你的脚本是写入该字段的最后一件事。 And/or you can try running it on window.load event.和/或您可以尝试在 window.load 事件上运行它。

If I run this from the console, it works: document.getElementsByClassName("_56AraZ")[0].value = "my username";如果我从控制台运行它,它可以工作: document.getElementsByClassName("_56AraZ")[0].value = "my username";

So probably you run your code before the input is even created.因此,您可能会在创建输入之前运行您的代码。 Let's check it.让我们检查一下。 Inside the setTimeout callback, insert this:在 setTimeout 回调中,插入:

const field = document.querySelector("._56AraZ");
if (field) {
    field.value = "my username";
} else {
    alert('No input field!');
}

This should let you know if you try to fill the field too early.如果您尝试过早填写该字段,这应该会让您知道。 If this is the case, you'll have to know somehow when the input field is being created and run your code after it happens.如果是这种情况,您必须以某种方式知道何时创建输入字段并在它发生后运行您的代码。 How to do it exactly - I can't tell you as I know nothing about your site's architecture.如何做到这一点 - 我不能告诉你,因为我对你网站的架构一无所知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM