简体   繁体   English

CasperJS脚本找不到正确的按钮和onclick-event

[英]CasperJS script does not find correct button and onclick-event

my casperjs script plays a video, then the webpage present 2 buttons either to continue viewing or to click "fill". 我的casperjs脚本播放了一个视频,然后该网页上显示了2个按钮以继续查看或单击“填充”。 The "fill option is what I am after but the buttons have same id, except for the desired option has an acivate-access() function: “填充选项是我想要的,但是按钮具有相同的ID,除了所需的选项具有acivate-access()函数:

button id="activate" class="btn btn lg" onclick="activate_access();

I tried doing this.click('#activate') and this.click('#activate.btn.btn-lg') but that just reloads a new video so I figure casper.evaluate or other approach is needed and how do I pass the correct onclick event(which is acivate_access();) to casperjs? 我尝试做this.click('#activate')和this.click('#activate.btn.btn-lg'),但这只是重新加载了一个新视频,所以我认为需要casper.evaluate或其他方法,我该怎么办?通过正确的onclick事件(这是acivate_access();)到casperjs? Code: 码:

var casper = require('casper').create({
            logLevel: "warning",
            verbose: true,
            onPageInitialized: function() {
                                            console.log("page opened");
                                            },

});
casper.userAgent('Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.36  (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36');
casper.viewport = {width:1200, height:800}; 
casper.start(url, function() {

    casper.wait(1000, function() {                             

                    casper.capture('look.png');
                    console.log('did screenshot');

    });


});

casper.wait(4000, function() {
console.log('clicking play');
this.click('#video');
casper.wait(37000);
casper.capture('play.png');

pres_url=this.getCurrentUrl();    

console.log(pres_url);
});

casper.evaluate(function() {
document.getElementById('activate').click();

});

casper.wait(500);
casper.run();

You don't have go into the page context and use the DOM API or jQuery to click this element. 您无需进入页面上下文并使用DOM API或jQuery单击此元素。 CasperJS' click(selector) is perfectly capable of handling this with the appropriate selector. CasperJS的click(selector)完全可以使用适当的选择器进行处理。

CSS selectors supports matching elements based on their attribute. CSS选择器根据其属性支持匹配的元素。 You use 你用

casper.click("button[onclick='activate_access();']");

for a perfect match or even 完美搭配甚至

casper.click("button[onclick*='activate_access']");

for a partial match. 部分匹配。

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

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