微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从javascript onclick返回值到变量,或者用onclick更改全局变量

我正在尝试获取网站上点击的元素(任何元素)的文本值,将其从 javascript返回到Selenium( Python),并使用该信息将每个点击的元素放在我的日志中.是否可以实现使用硒和 JavaScript

功能可以显示元素,但不会返回任何内容(正常的onclick行为).我知道我可以使用硒读取警报弹出窗口中的文本,但这会让浏览器在每次鼠标点击时闪烁.

function getEl()
{
   var ev = arguments[0] || window.event,origEl = ev.target || ev.srcElement;
   alert(origEL.text)
}

document.onclick = getEl;

试图访问javascript console.log现在似乎不起作用,所以使用console.log不是正确的答案.

我可以通过在点击事件上向localstorage写东西,然后在python中检查localstorage中的值来检测某个元素何时被点击.但它不是普遍的.我需要找到并设置我想观察的每一个元素.

function find_element() 
{
   aTags = document.getElementsByTagName("a");
   searchText = "comments";
   for (var i = 0; i < aTags.length; i++) 
   {
      if (aTags[i].textContent == searchText) 
      {
         found = aTags[i];
         return found;
      }
   }
}

found=find_element();

function saveEvent()
{
   localStorage.setItem("ZAD1.1","1"); 
}

if(found)
{

   found.addEventListener("click",saveEvent,false);

   var x=localStorage.getItem("ZAD1.1");
   if (x=="1")
   {
       count="comments link was clicked";
       return count;
   } 
}

之后,您从python硒调用javascript

z=driver.execute_script(javascript1)
if z=="comments link was clicked":
        #do something with this information

有没有办法获取用户在浏览器中点击的对象的信息?我在Firefox中使用Selenium.

编辑:
您可以使用getEL()获取每个点击的元素,并在localstorage中写入每个onclick输出.

localStorage.setItem(origEl.text,origEl.text);

创建本地存储,然后将temp写为数组或字符串

var temp="VISITED LINKS : "
for (var i = 0; i < localStorage.length; i++){
temp +=localStorage.getItem(localStorage.key(i))+" ";}
return temp

然后你可以将整个列表返回到python.

你能想到任何其他方式将点击的对象发送到python吗?

解决方法

如果我正确理解这一点,您只是在页面上的任何元素被点击时尝试收到通知,以便您可以使用该信息进行某些操作

你想要一个全局事件处理程序.

在纯香草JavaScript中,就像这样:

document.body.onclick = function(e) {
    console.log(e);           // `e` is the click event
    console.log(e.target);    // `e.target` is the element that was clicked
    // do something with this information
}

当你想“用这些信息做某事”时,你有很多选择,就像你似乎已经解决的那样.您可以在这里做的最好的事情是将AJAX请求发送到您所控制的HTTP服务器,然后,接收服务器端脚本可以将详细信息记录到文件(或使用它执行的任何操作).

您的代码表示,当点击某些东西时,您希望将其保存到localStorage – 我建议您保存字符串的base64编码的JSON,其中包含每个点击事件的目标元素.这里有一些代码可以用来做到这一点:

/**
 * Constant. This is the (string) key name of the 
 * save location in localStorage.
 */
var locationName = "myNamespace_clicks";

/**
 * Given an object,will JSON- and base64-encode
 * that object so it can be stored in localStorage.
 */
function encodeForStorage(dataObject) {
    return btoa(JSON.stringify(dataObject));
}

/**
 * Given a string from localStorage,will decode that
 * string from JSON and base64 to a JS object.
 */
function decodeStoredobject(dataString) {
    return JSON.parse(atob(dataString));
}

/**
 * Given an item,will add this item to the data stored
 * in localStorage,and then save the new data.
 */
function addItemToStorage(item) {
    var rawStoredData = localStorage.getItem(locationName);
    var dataObject;

    if(rawStoredData) {
        dataObject = decodeStoredobject(rawStoredData);
    }
    else {
        dataObject = {
            "clicks": []
        }
    }

    dataObject["clicks"].push(item);

    localStorage.setItem(locationName,encodeForStorage(dataObject));
}

document.body.onclick = function(e) {
    addItemToStorage(e.target);
}

原文地址:https://www.jb51.cc/js/154678.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐