2012-11-23 120 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

相关问题