2012-11-23 115 views
10

我寫了PhantomJS應用程序的一些部分。我正在解析一個網站,我正在寫用戶名和密碼到一個公式。在此之後,我必須點擊鏈接。而我得到這個錯誤:PhantomJS點擊頁面上的鏈接

TypeError: 'undefined' is not a function (evaluating 'myLink.click()') 

    phantomjs://webpage.evaluate():11 
    phantomjs://webpage.evaluate():22 
    phantomjs://webpage.evaluate():22 

這是我的PhantomJS代碼:

if(document.getElementById("m_Content_submitbtn2").getAttribute('data-role') == "button"){ 
     var myLink = document.getElementById("m_Content_submitbtn2"); 
    myLink.click(); 
} 

這是我的鏈接:

<div class='button'><a href="#" data-role="button" tabindex='0' onclick='WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;m$Content$submitbtn2&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true)); return false;' id="m_Content_submitbtn2">Log ind</a></div>&nbsp; 
+1

[PhantomJS的可能重複;點擊一個元素](http://stackoverflow.com/questions/15739263/phantomjs-click-an-element) – Bergi

回答

6

您嘗試使用click()方法上<a>元素在默認情況下在所有瀏覽器中都不受支持。相反,嘗試使用類似this而不是myLink.click();,您將不得不做eventFire(myLink, 'click');

+3

這只是給了我:ReferenceError:找不到變量:eventFire – mikkeljuhl

+0

沒有看到有一個鏈接那裏。抱歉。 – mikkeljuhl

+0

幫助。試圖在phantomjs中使用它並獲取參考錯誤,無論如何。 –

2

或者包括jQuery和做類似:

var page = require('webpage').create(); 
page.open('http://www.sample.com', function() { 
    page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() { 
    page.evaluate(function() { 
     $("button").click(); 
    }); 
    phantom.exit() 
    }); 
}); 
+0

'page.includeJS'正常工作,但是我得到一個'ReferenceError:Can not find variable:$'。 – loretoparisi

+0

...好的,我需要通過'page.evaluate'切換到'document'頁面上下文。在那一點上,它獲得'$'但導航不會發生 - 我沒有得到'onNavigationRequested'回調 – loretoparisi

相關問題