简体   繁体   English

单击Electron Webview中的元素

[英]Click on element in Electron webview

I want to click an element (button) several times in a remote website loaded through webview in Electron. 我想在通过Electron的webview加载的远程网站中多次单击一个元素(按钮)。

The following works when I enter it in the console (via inspect element): 当我在控制台中输入它时(通过inspect元素),以下工作:

a = setInterval( function(){
    var elem = document.getElementsByClassName("myclass");
    elem[0].click()
},1000)

It doesn't when I use it in Electron main script: 当我在Electron主脚本中使用它时,它不是:

window.webContents.executeJavaScript('a = setInterval( function(){ var elem = document.getElementsByClassName("myclass"); elem[0].click() },1000)', true);

I get an error: 我收到一个错误:

Uncaught TypeError: Cannot read property 'click' of undefined at :1:110 Uncaught TypeError:无法读取未定义的属性“ click”:1:110

I also tried the scipt preloaded in webview tag, but no luck. 我也尝试了预加载在webview标签中的scipt,但是没有运气。

What am I missing, or doing wrong? 我想念的是什么,还是做错了?

chromiumVersion: "58.0.3029.110"
electronVersion: "1.7.9"
nodeVersion:"7.9.0"

EDIT 编辑

Testing with Google.com and the speech icon in the searchbar: 使用Google.com和搜索栏中的语音图标进行测试:

var element =  document.getElementsByClassName('gsst_a');
if (typeof(element) != 'undefined' && element != null) {
  console.log('yep, element is found');
  console.log(element);
  console.log(element[0]);
  a = setInterval(
    function(){
      var elem = document.getElementsByClassName("gsst_a");
      elem[0].click()
      },1000)
} else {
  console.log('nope, element is not found');
}

This clicks the icon every 1 second in Chrome when entered in console. 在控制台中输入后,这会每1秒在Chrome中点击一次图标。

When I have my webview set to Google.com and have the following line, it still finds the element, but gives the error mentioned earlier again: 当我将Webview设置为Google.com并具有以下行时,它仍会找到元素,但再次给出前面提到的错误:

window.webContents.executeJavaScript('var element=document.getElementsByClassName("gsst_a");void 0!==element&&null!=element?(console.log("yep, element is found"),console.log(element),console.log(element[0]),a=setInterval(function(){document.getElementsByClassName("gsst_a")[0].click()},1e3)):console.log("nope, element is not found");', true);

console.log(element) gives: HTMLCollection(0) console.log(element)给出: HTMLCollection(0)

console.log(element[0]) gives: undefined console.log(element [0])给出: 未定义

Why can I enter the js in my normal browser, but not in Electron webview? 为什么我可以在普通浏览器中输入js,但不能在Electron Webview中输入js?

To answer my own question. 回答我自己的问题。

The event's referring to the web page in the BrowserWindow, not the webview within that. 该事件指的是BrowserWindow中的网页,而不是其中的webview。 So the element doesn't exist in the scope I was looking in, and I needed to do something similar within the BrowserWindow. 因此,该元素在我正在查找的范围中不存在,因此我需要 BrowserWindow中执行类似的操作。

Code: 码:

<html>
<head>
  <style type="text/css">
    * { margin: 0; }
    #browserGoogle { height: 100%; }
  </style>
</head>
<body>
  <webview id="browserGoogle" src="https://google.com"></webview>
  <script>
    const browserView = document.getElementById('browserGoogle')
    browserView.addEventListener('dom-ready', () => {
      const browser = browserView.getWebContents()
      browser.setDevToolsWebContents(devtoolsView.getWebContents())
      browser.webContents.executeJavaScript('var element=document.getElementsByClassName("gsst_a");void 0!==element&&null!=element?(console.log("yep, element is found"),console.log(element),console.log(element[0]),a=setInterval(function(){document.getElementsByClassName("gsst_a")[0].click()},1e3)):console.log("nope, element is not found");', true);
    })
  </script>
</body>
</html>

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

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