加载线上网站时,怎么获取到webview里的元素,我这获取不到,希望大神指教啊!我是新手都还不太懂
用preload 可以获取到。
<webview id="foo" src="https://www.github.com/" autosize="on" minwidth="576" minheight="432" preload="./test.js"></webview>
test.js
const waitForExternal = setInterval(() => {
if (document.querySelector('.btn')){
clearInterval(waitForExternal);
console.log(11111);
}else{
console.log('no');
}
}, 100);
可以获取到github上的 btn按钮, 不过控制台需要另外打开。
打开控制台代码,写在html里即可
<script>
onload = function() {
var webview = document.getElementById("foo");
webview.addEventListener("dom-ready", function(){
webview.openDevTools() // 这里! 打开 webview的控制台
});
}
</script>
参考: electron API