Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript XPath无法从Greasemonkey脚本中单击按钮_Javascript_Button_Xpath_Click_Greasemonkey - Fatal编程技术网

Javascript XPath无法从Greasemonkey脚本中单击按钮

Javascript XPath无法从Greasemonkey脚本中单击按钮,javascript,button,xpath,click,greasemonkey,Javascript,Button,Xpath,Click,Greasemonkey,我想打开一个包含单词google的链接。看起来是这样的: <input class="submit" style="background: #409999; border-radius: 10px;" value="open" onclick="Open('143615', '1', 'https://www.google.de/');" type="submit"> 它没有效果。我想通过Greasemonkey打开窗口(单击事件?) 当我使用alert(elm.href)时它说

我想打开一个包含单词
google
的链接。看起来是这样的:

 <input class="submit" style="background: #409999; border-radius: 10px;" value="open" onclick="Open('143615', '1', 'https://www.google.de/');" type="submit">
它没有效果。我想通过Greasemonkey打开窗口(单击事件?)


当我使用
alert(elm.href)时它说它是“未定义的”。但是当我在FirePath中尝试XPath时,XPath可以工作。

你说XPath可以工作,但是
elm.href
在GM脚本中没有定义。这表明
是通过AJAX添加的

您的脚本需要使用。比如:

// ==UserScript==
// @name     _Clicking "Open" buttons
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements ("input.submit[onclick*='open']", clickOpenBtn);

function clickOpenBtn (jNode) {
    triggerMouseEvent (jNode[0], "click");
}

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}

您说XPath可以工作,但是GM脚本中未定义
elm.href
。这表明
是通过AJAX添加的

您的脚本需要使用。比如:

// ==UserScript==
// @name     _Clicking "Open" buttons
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements ("input.submit[onclick*='open']", clickOpenBtn);

function clickOpenBtn (jNode) {
    triggerMouseEvent (jNode[0], "click");
}

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}