【问题标题】:How can I write some javascript to click this "continue" button?如何编写一些 javascript 来单击此“继续”按钮?
【发布时间】:2017-11-20 15:59:52
【问题描述】:
<span id="continue" class="a-button a-button-span12 a-button-primary"><span class="a-button-inner"><input id="continue" tabindex="5" class="a-button-input" type="submit" aria-labelledby="continue-announce"><span id="continue-announce" class="a-button-text" aria-hidden="true">
          Continue
        </span></span></span>

在页面部分的 HTML 上方,该页面有一个“继续”按钮,我正在尝试使用我的脚本单击该按钮。

所以,用 Javascript 编写,我正在尝试单击此按钮。但我没有尝试过任何工作。

我尝试的答案是:

function() {


      var goButton = document.getElementById("continue");
     goButton.click();},

为什么它不起作用?请帮帮我 !

【问题讨论】:

    标签: fluid-mac-app-engine greasekit


    【解决方案1】:

    您已将跨度和输入字段的 ID 设置为“继续”。 ID 对于单个元素应该是唯一的。当我在浏览器的控制台中输入您的代码时,它会返回以下内容:

    > var goButton = document.getElementById("continue");
    < undefined
    > goButton.valueOf()
    < <span id="continue" class="a-button a-button-span12 a-button-primary">
    

    您可以看到 span 是被选中的元素,而不是输入提交按钮。您应该重命名这两个元素中的任何一个,以便它们都有一个唯一的 ID 并在您的脚本中使用它。

    编辑:OP 提到 HTML 无法更改,因此可以使用此 Javascript,而不是固定使用非唯一 ID:

    function() { 
      var continueSpan = document.getElementById("continue"); 
      var goButton = continueSpan.firstElementChild.firstElementChild; 
      goButton.click();}
    

    【讨论】:

    • 我无法更改页面上的 HTML - 已修复。
    • 它不会变得更漂亮,但是您可以选择跨度并使用 selectChildElement 函数导航到输入字段。 var goButton = document.getElementById("continue"); goButton.firstElementChild.firstElementChild.valueOf() 输出将是:&lt;input id="continue" tabindex="5" class="a-button-input" aria-labelledby="continue-announce" type="submit"&gt;
    • 我不太清楚换行符在哪里。我还是把goButton.click();},放在最后吗?
    • valueOf 行只是为了显示您尝试执行的操作的结果。所以你需要的实际脚本是这样的:function() { var continueSpan = document.getElementById("continue"); var goButton = continueSpan.firstElementChild.firstElementChild; goButton.click();},
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 2014-03-28
    • 2014-01-04
    • 2016-01-07
    相关资源
    最近更新 更多