英文:
Trying to automate web site login presenting userid/password login with placeholders with Chrome Extension MV3 and vanilla Javascript
问题
我正在进行一个个人项目,旨在通过开发一个Chrome扩展程序来自动化两个不同网站上的相对复杂的工作流程,采用MV3;这包括与网站的交互,以及与Dropbox和GoogleSheets的读写交互。
我已经成功完成了与我的第一个网站的完整工作流程(即登录网站,浏览不同页面,与不同的动态DOM元素进行交互以输入信息,下载、上传、复制和移动文件到/从Dropbox,更新我的Google表格等等)。
现在我需要与第二个网站进行交互,但我卡在了我认为不应该是问题的地方:只是登录到网站。
第二个网站的登录页面见似乎相当标准;它呈现一个表单(带有id“UsernameAndPassword”),包括两个输入字段(带有id“username”和“password”)和一个按钮(带有id“loginFormUsernameAndPasswordButton”)
然而,输入字段默认显示一些占位字符串值,即“e-mail”和“Mot de passe”。
通过人工操作,实际点击表单字段会清除默认值,然后您可以输入信息并成功单击“登录”按钮。
我知道可以自动化这个登录流程,因为我使用Roboform密码管理器,它可以轻松登录到该网站。
然而,
当我在我的Chrome扩展内容脚本中设置这些字段时,使用document.getElementById("username").value = "dummyusername")和document.getElementById("username").value = "dummypassword"),这些设置的值确实显示出来,但在默认占位符之上有点“被覆盖”(即默认占位符和我设置的值都可见);
这是从DevTools控制台看到的情况
查看图像说明
如果我然后使用document.getElementById("loginFormUsernameAndPasswordButton").click()来触发表单按钮的点击操作,那么我用我的脚本设置的这些值将被清除,并且我会收到与用户名/密码字段为空的情况相对应的错误消息(就像我没有设置它们一样)。
我也尝试先将焦点设置到这些字段,但似乎没有任何区别。
你们能否帮助或提供如何处理这个问题的指针?非常感谢!
英文:
I'm on this personal project to automate a relatively complex workflow across two different web sites by developing a Chrome extension under MV3; this includes site interaction, along with Dropbox and GoogleSheets read & write interactions.
I've successfully accomplished the full workflow with my first web site (namely logging in to the site, navigating through different pages while interacting with different dynamic DOM elements to enter information, downloading, uploading, copying and moving files from/to Dropbox, updating my Google Sheet, etc.)
I now need to interact with the second web site, and I am stuck with what I believe should be a non-issue: just logging in to the website.
The login page of the second site see seems quite standard; it presents a form (with id "UsernameAndPassword") including two input fields (with id's "username" and "password") and a button (with id "loginFormUsernameAndPasswordButton")
The input fields, however, show some kind of placeholder string values by default, namely "e-mail" and "Mot de passe".
With human interaction, actually clicking on the form fields clears up the default value, and then you can type your information and click the "login" button form successfully.
I know it is possible to automate this login flow because I use Roboform password manager and it does login to the site with no sweat.
HOWEVER,
when I set those fields in my chrome extension content script by using document.getElementById("username").value = "dummyusername") and document.getElementById("username").value = "dummypassword"), those set values do appear but kind of "overwritten" on top of the default placeholders (i.e, both the default placeholders AND the values I set are visible);
This is what it looks like from de DevTools console
enter image description here
If I then trigger the click action on the form button by using document.getElementById("loginFormUsernameAndPasswordButton").click(), those values I set with my script are cleared up and I get the error message corresponding to the situation where the username/password fields are blank (as if I had not set them).
I have also tried to first set the focus to those fields, but it does not seem to make any difference.
Can you guys please help or give pointers on how to deal with this? Thank you in advance!
答案1
得分: 0
wOxxOm在他的评论中提到(使用不推荐使用的document.execCommand并检查此示例),解决了我的问题。
我不知道如何接受您的评论作为答案,或者如何适当地感谢您的帮助,所以我将我解决问题的方法发布为答案,感谢您的帮助。
事实上,我需要更多地感谢您,因为我相信我在过去几个月中通过分析您的建议、解释和/或示例指向,已经解决了大约80%的个人项目中最困难的情况。
在我的情况下,以下内容可以设置这些输入字段:
let userid = "myuserid"
let password = "mypassword"
document.getElementById("userid").focus()
document.execCommand('insertText', false, userid)
document.getElementById("password").focus()
document.execCommand('insertText', false, password)
document.getElementById("login_button").click()
英文:
wOxxOm's pointer in his comment (to use the deprecated document.execCommand and check this example) did solve my problem.
I don't see how to accept your comment as the answer or to properly thank you for your help, so I'm posting as an answer what I did to solve my issue thanks to your help.
In fact, I do need to thank you much more, since I believe I must have solved around 80% of the most difficult situations in my personal project over the past months by analyzing your tips, explanations and/or pointers to examples.
This is what works in my case to set those input fields:
let userid = "myuserid"
let password = "mypassword"
document.getElementById("userid").focus()
document.execCommand('insertText', false, userid)
document.getElementById("password").focus()
document.execCommand('insertText', false, password)
document.getElementById("login_button").click()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论