2017-03-01 125 views
0

我已經嘗試過casperJS中的各種方法來填寫和提交表單。代碼如下所示。最終,我正在建造一個機器人,以自動檢查IAG Cargo門戶網站上的貨物空運提單的狀態。使用Casperjs提交表格

Sendkeys將完成表單,但我無法設置單擊「搜索」按鈕。

使用casperJS表格填充方法根本不起作用。

這是一個不尋常的網站或我做錯了什麼?

在程序下面的代碼出現失敗上線

this.clickLabel(「搜索」,「按鈕」);

和後續的代碼不運行。

(我已經使用了虛擬運單號在這個例子中,因此最終的頁面將顯示「運單未找到」)

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

var x = require('casper').selectXPath; 

phantom.cookiesEnabled = true; 

casper.userAgent('Mozilla/4.0 (compatable; MSIE 6.0; Windows NT 5.1)'); 

casper.start('https://www.iagcargo.com/iagcargo/portlet/en/html/601/main/search'); 

casper.waitForSelector("#awb_cia", function() { 

    this.echo('Selector found'); 
    casper.capture('iag_start.png'); 

    this.sendKeys('#awb_cia','125'); 
    this.sendKeys('#awb_cod','12345675'); 

}); 

casper.then(function step2() { 

    this.clickLabel('SEARCH', 'button'); 

    this.echo('this is step 2');  

    casper.capture('iag_end.png'); 
    }); 


require('utils').dump(casper.steps.map(function(step) { 
    return step.toString(); 
})); 


casper.run(); 

回答

0

不能夠點擊的是,可能有很多原因,一個共同的問題。 clickLabel()在這裏不起作用,因爲它不是一個按鈕標籤,而是一個輸入標籤。這是我在頁面上看到的:

您可以試試這個, casper.click('input [value =「Search」]');

一般來說,這裏是點擊元素時可以嘗試的東西。

  1. 嘗試更改viewportSize,例如 viewportSize:{width:1024,height:768}

這是因爲,casperjs不會在頁面上看到任何不可見的內容。

  1. 如果使用xpath選擇器,請嘗試CSS。 XPath選擇器很脆弱,並且在所有瀏覽器中都不起作用。

  2. 有時jquery點擊工作,例如,

    casper.evaluate(函數(){ VAR元素= document.querySelector( 'span.control.critical.closer'); $(元件)。單擊(); });

  3. 如果按鈕標籤內有一個函數,則可以直接調用該函數。例如。 SEARCH 您可以直接調用該函數, casper.someFunc();

0

var casper = require('casper').create({ 
 
    verbose: true, 
 
    logLevel: "debug", 
 
    waitTimeout: 60000, 
 
    resourceTimeout: 10000, 
 
    viewportSize: { 
 
    width: 1024, 
 
    height: 768 
 
    }, 
 
    pageSettings: { 
 
    javascriptEnabled: true, 
 
    loadImages: true, 
 
    loadPlugins: true 
 
    } 
 
}); 
 

 
var x = require('casper').selectXPath; 
 

 
phantom.cookiesEnabled = true; 
 

 
casper.userAgent('Mozilla/4.0 (compatable; MSIE 6.0; Windows NT 5.1)'); 
 

 
casper.start('https://www.iagcargo.com/iagcargo/portlet/en/html/601/main/search'); 
 

 
casper.waitForSelector("#awb_cia", function() { 
 

 
    this.echo('Selector found'); 
 
    casper.capture('iag_start.png'); 
 

 
    this.sendKeys(x('//*[@id="awb_cia"]'),'125'); 
 
    this.sendKeys('#awb_cod','12345675'); 
 
    this.capture('iag_middle.png'); 
 

 
}); 
 

 
casper.then(function step2() { 
 

 
    if(this.exists('body > div.cuerpo > div.cuerpo > div.contenido > div > form > table > tbody > tr > td:nth-child(3) > input')){ 
 

 
    this. click('body > div.cuerpo > div.cuerpo > div.contenido > div > form > table > tbody > tr > td:nth-child(3) > input'); 
 

 
    } 
 

 

 
}); 
 
casper.then(function step3() { 
 
    this.echo('this is step 2'); 
 

 
    casper.capture('iag_end.png'); 
 
}); 
 

 
//require('utils').dump(casper.steps.map(function(step) { 
 
// return step.toString(); 
 
//})); 
 

 

 
casper.run();

+0

這工作你需要使用完整的行業路徑,然後單擊 –