2

编辑:这是 Windows 的行为,在 linux 中它只是失败了。

首先,如果您使用 casper 成功在 gmail 上导航(没有随机等待时间 - 从 20 秒到 5 分钟),请告诉我。

我想在我们的网站上注册,然后通过 Gmail 自动验证我的注册(整个注册步骤)。以前有人这样做过吗?

我注册没有问题,我可以登录我的邮箱(Gmail),但是在我在 Gmail 中导航和验证我的注册时遇到了一些麻烦,我观察到 phantomJS 和 slimerJS 之间的不同行为。

在幻像中它会工作(没有特殊命令),但可能需要 5 分钟才能通过下一步(waitForSelector)。使用 slimerjs,它只会停留在邮箱页面上。

编辑:奇怪的事情:如果我在打开弹出窗口的链接上手动单击(更苗条),它将停止被阻止并且我的导航继续,就像它无法检测到步骤本身的结束并且无法执行 waitFor提交点击后没有其他交互。是刷新/重新加载问题吗?

试试看自己:

casper.thenOpen('https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/&hl=en', function(){
    this.sendKeys("input#Email","your mail");
    this.sendKeys("input#Passwd","your password");
    this.click("input#signIn.rc-button-submit");
    console.log(this.getCurrentUrl());
    this.waitForSelector(".aeF",function(){//fail with linux -> timeout
        this.test.pass("ok"); //windows -> stuck in slimer, several times in phantom
        this.test.assertExists(".T-I.J-J5-Ji.T-I-KE.L3","Gmail Home ok");
        console.log("url "+this.getCurrentUrl());
    });

而且我没有收到任何超时错误。在 slimerjs 中,它只是保持页面打开。

如果我执行 waitForPopup 而不是 waitForUrl,则会出现错误(超时 -> 未弹出),那么为什么 waitForUrl/waitForSelector... 会卡住?我也尝试了 --web-security=no,--ignore-ssl-errors=true 命令(未链接,但我也尝试了 --output-encoding=ISO 8859-1 ,但它不起作用)。

这里幻影和苗条(doc)之间的区别:http://docs.slimerjs.org/0.8/differences-with-phantomjs.html( 认为在这个问题上没用)

4

1 回答 1

2

好吧,我们终于找到了一种方法:问题是默认情况下 ajax 请求上的 gmail 循环,检查一些新邮件等...参见Page polls remote url,导致 casper 一步步阻塞。

幸运的是,谷歌提出了一种避免这种情况的方法,使用简化的 HTML 版本(例如,您可以使用特殊的 gmail 地址来使用此版本进行测试): casperJs+gmail

这样脚本就可以正常工作了。

奖金 :

/*
 * Click on an element specified by its selector and a part of its text content.
 * This method resolves some problem as random space in textNode / more flexible too.
 * Need to fix one bug though : when there is a tag in textContent of our selector.
 */
casper.clickSelectorHasText = function (selector, containsText){
    var tmp = this.getElementsInfo(selector)
        ,i
        ,l
        ,bool=false
        ;
    for (i=0,l=tmp.length;i<l; i++){
        if(tmp[i].text && tmp[i].text.indexOf(containsText)!==-1){
            this.clickLabel(tmp[i].text);
            bool=true;
            break;
        }
    }
    casper.test.assert(bool, "clickSelectorHasText done, text and selector found -> click selector " + selector +" which contains text " + containsText);
};

casper.thenOpen('https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/&hl=en', function scrapeCode(){
        //log in
        this.sendKeys("input#Email","your email");
        this.sendKeys("input#Passwd","your password");
        this.click("input#signIn.rc-button-submit");
        //wait to redirect to our mailbox
        this.waitForSelector("form[name='f']",function(){
            //check main block
            this.test.assertExists("form[name='f']","Gmail Home ok");
            this.test.assertSelectorHasText("span", "Your gmail title message");
            this.clickSelectorHasText("font", "one string which appears in font tag");
            //wait inscription message appears
            this.waitForSelector("div.msg",function(){
                this.test.assertSelectorHasText("a","the message which activates your account--> in <a>");
            });
        })
        //validate our account
        .then(function(){
            this.clickLabel("the message which activates your account--> in <a>");
            this.waitForPopup(/mail/, function(){
                this.test.pass("popup opened");
            });
            this.withPopup(/mail/, function(){
                this.viewport(1400,800);
                this.test.pass("With Popup");
                //wait something on your website (for me selector .boxValid)
                this.waitForSelector(".boxValid", function(){
                    /*
                     * Here your code after validation
                     */
                });

            });
        })

使用 event 使用普通 gmail 可能会做到这一点,请参阅resource.received

于 2014-04-30T13:36:27.527 回答