我目前正在使用不同的视口(viewport)和代理在 NightmareJS 中进行测试。我正在自动访问一个具有大量复杂脚本和样式的网站。我无法在单独的测试浏览器中充分复制代理和 View ,以便该网站为我显示与我访问 Nightmare 时相同的内容。因此,我需要能够右键单击 Nightmare 的 Electron 窗口中的元素,以便可以检查 DOM 中的特定元素。
问题是 Electron 窗口中禁用了右键单击。我发现this answer其中描述了向渲染过程添加代码:
// Importing this adds a right-click menu with 'Inspect Element' option
const remote = require('remote')
const Menu = remote.require('menu')
const MenuItem = remote.require('menu-item')
let rightClickPosition = null
const menu = new Menu()
const menuItem = new MenuItem({
label: 'Inspect Element',
click: () => {
remote.getCurrentWindow().inspectElement(rightClickPosition.x, rightClickPosition.y)
}
})
menu.append(menuItem)
window.addEventListener('contextmenu', (e) => {
e.preventDefault()
rightClickPosition = {x: e.x, y: e.y}
menu.popup(remote.getCurrentWindow())
}, false)
注意:此代码可能需要按照 referenced answer 中所述进行更改。但是在使用 Nightmare 时我应该在哪里添加这样的代码?
据我所知,主脚本与 Electron 渲染过程脚本不同。至于那种解决方案,this可能会更好,但是如何调用?
最佳答案
目前 Nightmare 中右键没有可用的api,引用问题:
https://github.com/segmentio/nightmare/issues/346
一个可能的解决方案是直接使用 Electron 绕过nighmarejs。
示例(未测试):
webpage.sendEvent('contextmenu')
关于javascript - 如何在 NightmareJS 中启用右键单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43729849/