PhantomJS单击页面上的链接

时间:2012-11-23 23:14:15

标签: javascript 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;

2 个答案:

答案 0 :(得分:6)

您正尝试在click()元素上使用<a>方法,默认情况下,所有浏览器都不支持该方法。相反,请尝试使用this之类的内容,而不是myLink.click();,而是eventFire(myLink, 'click');。{/ p>

答案 1 :(得分: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()
  });
});