无需物理点击即可打开元素的contextMenu

时间:2019-04-28 14:47:03

标签: javascript jquery html xpath contextmenu

我试图操纵我在互联网上找到的这段代码。这对我单击链接很有用。但是在某些情况下,我需要使用右键单击功能打开该元素的contextMenu。

如果有一种方法可以将其转换为打开contextMenu,请参见此代码。

我试图用一些代码来操纵它,但是没有成功。

(function run (pXPathStr) {
  'use strict';

var ptyScriptName   = "Click on Link Identified by XPath"

/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:  Click on the Hyperlink Identified by the XPath provided by KM

RETURNS:  One of these, as text:
  • "OK" -- if successful, no errors

  • "[ERROR]" at start of results if a script error occurred.

KM VARIABALES REQUIRED:
  • xPath    -- the full XPath to the Link

    EXAMPLE XPATH:
    //*[@id="ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate"]

KM VARIABLES SET:
  • NONE

REF:
  • [Introduction to using XPath in JavaScript](https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
  //--- GET THE XPATH FROM KM ---
  var xPathStr = document.kmvar.xPath;

  var returnResults = "TBD";

  //--- GET THE HTML ELEMENT IDENTIFIED BY THE XPATH ---
  var elemFound =  document.evaluate(
      xPathStr, document, null, 0, null
    ).iterateNext();

  if (elemFound) {
    //--- CLICK ON THE LINK ---
    elemFound.click();
    returnResults = "";
  }
  else {
    //--- ERROR:  ELEMENT WAS NOT FOUND ---
    returnResults = '[ERROR] Element NOT FOUND for XPath:\n' + xPathStr;
    alert(returnResults);
  }

  return returnResults;

})();

/* 
*/

我希望运行此功能会打开元素的contextMenu。

我将在图像元素上使用contextMenu下载到我的桌​​面上。

这是在链接中单击鼠标右键后要打开的菜单的图像。 The is the action result in the image link I want to happen

Right - Click Menu I want to open after right Click, and the Highlighted blue color Is what I will click next. But I can easily call this by pressing Key "S" then enter to download image.

0 个答案:

没有答案