自动单击带有tampermonkey的网页上的元素?

时间:2019-02-11 06:51:28

标签: javascript html greasemonkey tampermonkey

我正在尝试从 Factiva 获取结果的最后一页。我希望自动单击按钮Next 100

提供更多信息的图像:image

我已经尝试了脚本AutoClicker。它适用于modify search按钮,但无法单击Next 100

错误显示为:

  

自动点击程序找不到点击对象!

我想知道我使用的 Xpath 是否错误。

元素

    <a class="nextItem" href="javascript:void(0)"
      onclick="viewNext('241');return false">Next 100 </a>

选择器

    #headlineHeader33 > table > tbody > tr > td > a.nextItem

Xpath

    //*[@id="headlineHeader33"]/table/tbody/tr/td/a[2]

这是我使用的代码:

(function() {
    'use strict';

var cyce = 1000;

var str_xpath = '//*[@id="headlineHeader33"]/table/tbody/tr/td/a[2]';
// another way,e.g: var str_qs = "div .search span a";
var str_qs = "";



function getTargetById(t_id){
    var target = document.getElementById(t_id);
    return target;
}


function getTargetByTAV(t_tag,t_attr,t_value){
    var target = document.getElementsByTagName(t_tag);
    for(var i=0;i <target.length;i++){
        if(target[i].getAttribute(t_attr) == t_value){
            return target[i];
        }
    }
}

function getTargetByXpath(str_xpath) {
    var xresult = document.evaluate(str_xpath, document, null, XPathResult.ANY_TYPE, null);
    var xnodes = [];
    var xres;
    while (xres = xresult.iterateNext()) {
        xnodes.push(xres);
    }
    return xnodes;
}


function getTargetByQS(str_qs){
    var target = document.querySelector(str_qs);
    return target;
}


function trim(str){
    str = str.replace(/(^\s*)|(\s*$)/g, "");
    return str;
}


var btn;

if(isCustiom === true){
    btn = getTargetByCustom();
}

if(trim(id) !== "" && (btn === null | typeof(btn) !== 'object')){
    btn = getTargetById(id);
}

if(trim(tag) !== "" && trim(attr) !== "" && value !== "" && (btn === null | typeof(btn) !== 'object')){
    btn = getTargetByTAV(tag,attr,value);
}

if(trim(str_xpath) !== "" && (btn === null | typeof(btn) !== 'object')){
    btn = getTargetByXpath(str_xpath)[0];
}

if(trim(str_qs) !== "" && (btn === null | typeof(btn) !== 'object')){
    btn = getTargetByQS(str_qs);
}

setInterval(function() {
        if (btn !== null && typeof(btn) === 'object') {
            console.info("[+] AutoClicker click object: " + btn.innerHTML);
            btn.click();
        }else{
            console.warn('[-] Autoclicker does not find the click object!');
        }
    },cyce);
})();

1 个答案:

答案 0 :(得分:0)

我无法帮助您使用xPath,因为我不知道文档的格式。但是,您的脚本还提供了一种使用querySelector选择元素的方法。使用css-selectors选择元素。 我强烈建议您使用这种更现代的解决方案

function getTargetByQS(str_qs){
    var target = document.querySelector(str_qs);
    return target;
}

您唯一需要做的就是提供str_qs和正确的选择器:

选择器

a.nextItem[onclick*='viewNext']

选择器将查找具有类名a的锚元素nextItem和包含onclick的属性viewNext

字符串

str_qs = "a.nextItem[onclick*='viewNext']";

查看实际的解决方案:

var timer;
(function() {
    'use strict';

//stripped all other functions just to demonstrate the function getTargetByQS

var cyce = 1000;

var str_qs = "a.nextItem[onclick*='viewNext']";

function getTargetByQS(str_qs){
    var target = document.querySelector(str_qs);
    return target;
}


function trim(str){
    str = str.replace(/(^\s*)|(\s*$)/g, "");
    return str;
}


var btn;
if(trim(str_qs) !== "" && (btn === null | typeof(btn) !== 'object')){
    btn = getTargetByQS(str_qs);
}

timer = setInterval(function() {
        if (btn !== null && typeof(btn) === 'object') {
            console.info("[+] AutoClicker click object: " + btn.innerHTML);
            btn.click();
        }else{
            console.warn('[-] Autoclicker does not find the click object!');
        }
    },cyce);
})();

//not part of the solution, just for the demo.
function viewNext(){
  window.clearInterval(timer); //shut down the interval for demo-purposes
}
  <a class="nextItem" href="javascript:void(0)"
      onclick="viewNext('241');return false">Next 100 </a>