首页 > 解决方案 > 如何通过 GM 用户脚本登录 neopets.com

问题描述

我创建了一个从 Neopets登录页面开始并自动填充用户名和密码的用户脚本。我无法让脚本单击巨大的绿色“登录”按钮。

$('input[name=username']).val('username_here');
$('input[name=password']).val('password_here');

我已经尝试了以下所有方法来单击登录按钮或提交表单。

//None of these clicked the sign in button:
$("input.submit[name = 'welcomeLoginButton']").submit();
$("input.submit[name = 'welcomeLoginButton']").click();
$('body').on('click','welcomeLoginButton',function(){alert('logged in?');});
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.submit[name = 'welcomeLoginButton']"))).click();

我知道没有名为“welcomeLoginButton”的元素,只有一个名为.welcomeLoginButton. 填写用户名和密码后,如何让表单自行提交?

标签: javascriptjquerygreasemonkey-4

解决方案


只需获取该类的所有元素的列表,welcomeLoginButton然后单击第 0 个索引。

jQuery版本:

$('.welcomeLoginButton')[0].click();

香草 JS 版本:

document.getElementsByClassName("welcomeLoginButton")[0].click();
// querySelector without the All just returns the first index of the array
document.querySelector('.welcomeLoginButton').click();
document.querySelectorAll('.welcomeLoginButton')[0].click();

推荐阅读