我如何用 javascript select 这个元素?

How can I select this element with javascript?

我正在尝试编写一个在启动时登录网站的 applescript 应用程序。到目前为止,我可以成功打开Chrome,将网页加载到活动选项卡,select并输入密码,但我无法点击提交按钮。

下面是页面上的元素:

<button type="submit" class="btn  btn--ac" alt="Login" 
   title="Login" tabindex="6"><i aria-hidden="true" 
   class="lush  lush-locked"></i> Login</button>

我在javascript的技能水平只有none,但我找不到成功select这个项目并点击它的方法。我已经尝试使用 TagName 和 ClassName 但它不起作用,也许我的语法不正确。有没有办法在没有 ID 类型的情况下做到这一点?

我试过:

execute front window's active tab javascript 
"document.getElementByClassName('btn, btn--ac').click()"

execute front window's active tab javascript 
"document.getElementByTagName('button').click()"

我应该注意到这个脚本在 Google Chrome.

中运行

提前感谢您的帮助。

首先你用错了函数。应该是

document.getElementsByClassName

尝试使用:(假设提交按钮是唯一带有 class btn 的按钮)

document.getElementsByClassName('btn')[0].click();

下面是Mozilla函数的基本定义:

document.getElementsByClassName():
Returns an array-like object of all child elements which have all of the given class names.

因此,您将获得一个元素列表。因此,你select其中的第一个元素(使用0作为索引),然后点击它。

在纯 js 中 select 他 class 的元素的正确语法是:

document.getElementsByClassName('btn--ac');

有了这个,您将获得一个包含所有元素的数组 class 'btw--ac'。在你的情况下试试这个:

document.getElementsByClassName('btn--ac')[0].click();