简体   繁体   English

为什么硒不能添加事件监听器?

[英]Why does selenium can't add event listener?

I tried this code (C#) : 我尝试了这段代码(C#):

string javascript = "window.document.addEventListener('domready', function(){alert('READY');}, false);console.log('test');"
((IJavaScriptExecutor)d).ExecuteScript(javascript);

The formatted Javascript code is : 格式化的Javascript代码为:

window.document.addEventListener(
  'domready', 
  function() {
    alert('READY');
  }, 
  false);
console.log('test');

The "READY" popup never show up. “ READY”弹出窗口永远不会显示。 Do you have an idea on how to solve this problem ? 您对如何解决此问题有想法吗?

The javascript is executed as soon as the page start loading (the console.log('test') command is executed before anything is displayed on the screen). 页面开始加载后立即执行javascript(在屏幕上没有显示任何内容之前执行console.log('test')命令)。

I believe that the READY popup never shows because the event listener is added after 'domready' fires its event. 我认为READY弹出窗口永远不会显示,因为在'domready'触发事件后添加了事件侦听器。 You might be able to leverage document.readyState === "complete" to achieve what you want, by simply doing something like: 您可以通过简单地执行以下操作来利用document.readyState ===“ complete”实现所需的功能:

if (document.readyState === "complete") {
  // do stuff
} else {
  window.document.addEventListener('domready', doStuff);
}

Hopefully this helps! 希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM