解決此問題需要我很多時間。下面 是我phantomjs代碼phantomjs getElementsByClass()在評估中無法獲取元素
var page = require('webpage').create()
page.open("https://www.google.com.hk/",function(status){
if(status === 'success'){
var aa = page.evaluate(function() {
return document.getElementsByClassName('ctr-p').length
})
console.log(aa)
}
phantom.exit(0)
})
正如你所看到的,在評價函數的返回值document.getElementsByClassName(「CTR-P」)的長度,當我將其複製到Chrome檢查控制檯,將打印「3 ',如下圖所示: 但是當我執行phantomjs代碼時,結果爲'0'!
這是什麼問題?請〜
對不起,延遲迴復。我將所有的代碼複製到我的Mac上,但也如上所示顯示0。 page.settings.userAgent ='Mozilla/5.0(Windows NT 6.1; WOW64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/51.0.2704.106 Safari/537.36'唯一的好處是它不會顯示一些惱人的提示,如: https ://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/voice/js/voice_1e62c0f.js:23支持 等 – suoyong
但你們給我一些建設性的意見,謝謝。 – suoyong
謝謝。什麼是您的PhantomJS版本?可能會老了。 – Vaviloff