【发布时间】:2017-02-21 13:35:09
【问题描述】:
编辑: 非常感谢您的所有努力,现在让我的所有代码运行起来!
我想触发对https://shop.adidas.ae/en/stan-smith-shoes/S82255.html 的点击(制作鞋机器人)。 为什么这段代码不能触发尺寸类型按钮?
setTimeout(function () {
$('select.product-type.js-size-type').trigger('click');
}, 1000);
这是选择按钮:
<select class="product-type js-size-type">
这就是 chrome 控制台所说的
caught TypeError: Cannot read property 'click' of null
at <anonymous>:2:41
更新:
我的 chrome 扩展清单:
{
"name": "name",
"description": "test",
"version": "1.0",
"manifest_version": 2,
"permissions": ["*://*/*"],
"content_scripts": [
{
"matches": ["*://*.adidas.ae/*"],
"js": ["jquery.js", "yeezyCopper.js"]
}
],
"background": {
"scripts": ["background.js"],
"persistent": false
}
}
YeezyCopper.js:
setTimeout(function () {
$('select.product-type.js-size-type').trigger('click');
}, 1000);
提前致谢!
【问题讨论】:
-
尝试将其放入就绪状态:
$(function(){ $('select.product-type.js-size-type').trigger('click'); }) -
1000 可能不够。使用更大的超时时间或 MutationObserver 来监视元素。 Is there a JavaScript/jQuery DOM change listener?
-
亚当,你的代码实际上什么也没做,所以控制台没有错误代码......
-
wOxxOm,我不认为超时应该是可能的,因为在输入控制台时页面已经加载。让我看看 MutationObserver...
-
如果您询问 chrome 扩展,您几乎必须发布 manifest.json 文件,并告诉我们哪个 js 文件包含您向我们展示的代码。但首先将其缩减为minimal reproducible example。
标签: javascript jquery google-chrome google-chrome-extension