2015-06-06 72 views
2

我有一個奇怪的問題,在屏幕抓取與spookyjs/capserjs。CasperJS/SpookyJS css選擇器是存在和不存在

我想從以下網站獲取信息:'https://www.rwe-smarthome.de/is-bin/INTERSHOP.enfinity/WFS/RWEEffizienz-SmartHome-Site/de_DE/-/EUR/ViewApplication-DisplayWelcomePage'。

由於該網站包含多個產品頁面,我還想打開其他網站。

通常你可以使用

this.click(selector, function() {}); 

實現這一目標。

由於一些奇怪的原因,這裏不起作用。

請看看下面的代碼:

var selector1 = "div#workingTemplate div:first-of-type ul.linkList li:nth-child(2) a"; 

spooky.waitUntilVisible(selector1); 
spooky.thenClick(selector1); 
spooky.wait(500); 
spooky.then(function() { 
    this.capture("RWETest-02.jpg"); 
}); 

我收到一個錯誤

CasperError: Cannot dispatch mousedown event on nonexistent selector: div#workingTemplate div:first-of-type ul.linkList li:nth-child(2) a 

這很奇怪,因爲如果選擇/ DOM對象不存在,它應該會失敗在waitUntilVisible()

而且當我嘗試檢查,如果選擇存在,答案似乎是肯定的,因爲我也得到與不存在選擇的錯誤:

代碼:

spooky.then([{sel: selector1},function() { 
    if(this.exists(sel)) { 
     this.click(sel); 
     this.wait(500); 
     this.then(function() { 
      this.capture("RWETest-02.jpg"); 
     }); 
    } 
    else { 
     this.emit("logMessage", "Selector does not exists..."); 
    } 
}]); 

錯誤:

CasperError: Cannot dispatch mousedown event on nonexistent selector: div#workingTemplate div:first-of-type ul.linkList li:nth-child(2) a 

因爲SpookyJS我使用PhantomJS 1.9.7和CasperJS 1.1.0-beta3。

有沒有人有這方面的想法?

回答

2

這很可能與PhantomJS 1.x中的錯誤有關,該錯誤無法正確找到基於使用:nth-child()的CSS選擇器的元素。有關更多信息,請參閱this question

由於CasperJS支持幾乎所有功能的XPath表達式,可以翻譯CSS選擇器XPath表達式:

var xpathExpr1 = "//div[@id='workingTemplate']//div[1]//ul[contains(@class,'linkList')]//li[2]//a"; 

然後,您可以使用它像這樣:

var selectXPath = 'xPath = function(expression) { 
    return { 
    type: "xpath", 
    path: expression, 
    toString: function() { 
     return this.type + " selector: " + this.path; 
    } 
    }; 
};' 
... 
spooky.then([{x: selectXPath}, function() { 
    eval(x); 
    this.waitUntilVisible(xPath(xpathExpr1)); 
    this.thenClick(xPath(xpathExpr1)); 
    ... 
]); 

的問題在於SpookyJS不公開XPath實用程序,因此您需要執行一些描述爲in GitHub isse #109的解決方法。

+1

太好了,非常感謝你!對於任何與Webstorm和此解決方案相處的人來說,selectXPath必須放在一行中,否則會出錯。 – solick