簡體   English   中英

單擊按鈕應該會得到 Ajax 響應,但會在 CasperJS 中重新加載整個頁面

[英]Clicking a button should get an Ajax response, but reloads the whole page in CasperJS

我正在玩 CasperJS 並試圖在https://registrierung.web.de/#.homepage.loginbox_1.1.registrierung上捕獲一些免費電子郵件別名

所以我有輸入字段:“E-Mail-Wunschname:”,我想在其中粘貼一個名稱,然后單擊“Prüfen”按鈕,然后只抓取建議的帳戶。

到目前為止,我已經嘗試過:

var casper = require('casper').create({
    pageSettings: {
        loadImages: false,
        loadPlugins: true,
        userAgent:('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:45.0) Gecko/20100101 Firefox/45.0')
    }

});

var mouse = require("mouse").create(casper);

casper.start('https://registrierung.web.de/#.homepage.loginbox_1.1.registrierung').viewport(1200,1000);

casper.then(
    function() {               
        this.sendKeys('.wishname.feedback-panel-trigger.multiReplaceCharsInWishnamelField',"Test");
        this.sendKeys('.wishname.feedback-panel-trigger.multiReplaceCharsInWishnamelField',casper.page.event.key.Enter);       
        this.wait(5000);        
    }
);
casper.then(function() {
    this.wait(5000);
    this.capture('webde.png');
    console.log('clicked ok, new location is ' + this.getCurrentUrl());
});


casper.run();

我也厭倦了點擊按鈕:

casper.wait(6000, function() {
    this.evaluate(function(){                 
        document.querySelector('.wishname.feedback-panel-trigger.multiReplaceCharsInWishnamelField').value = "Test";           
        document.querySelector('#checkAvailabilityBtn').click();
            });
    });
    casper.then(function() {
        this.capture('webde.png');
        console.log('clicked ok, new location is ' + this.getCurrentUrl());
    });

兩種方式都只是完整提交頁面,而不僅僅是生成建議。

單擊按鈕( casper.click("#checkAvailabilityBtn") )似乎效果很好。

這是完整的腳本:

var casper = require('casper').create();

casper.start('https://registrierung.web.de/#.homepage.loginbox_1.1.registrierung').viewport(1200,1000);

casper.then(function() {               
    this.sendKeys('.wishname.feedback-panel-trigger.multiReplaceCharsInWishnamelField', "Test"); 
    this.click("#checkAvailabilityBtn");
});
casper.wait(5000);
casper.then(function() {
    this.capture('test80_webde.png');
    console.log('clicked ok, new location is ' + this.getCurrentUrl());
});

casper.run();

順便說一句, casper.sendKeys()無法處理諸如 Enter 鍵之類的按鍵操作。 您需要使用 PhantomJS 的page.sendEvent()函數。 它按以下方式正常工作,但在這種情況下似乎無法正常工作,因為它會重新加載頁面:

this.sendKeys('.wishname.feedback-panel-trigger.multiReplaceCharsInWishnamelField', "Test", {keepFocus: true}); 
this.page.sendEvent("keypress", this.page.event.key.Enter);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM