最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PhantomJS click a link on a page - Stack Overflow

programmeradmin3浏览0评论

I have written some parts of a PhantomJS application. I am parsing on a website where I am writing username and password to a formular. After this I have to click on a link. Whereas I get this error:

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

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

This is my PhantomJS code:

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

And this is my link:

<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;

I have written some parts of a PhantomJS application. I am parsing on a website where I am writing username and password to a formular. After this I have to click on a link. Whereas I get this error:

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

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

This is my PhantomJS code:

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

And this is my link:

<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;
Share Improve this question asked Nov 23, 2012 at 23:14 mikkeljuhlmikkeljuhl 4001 gold badge5 silver badges13 bronze badges 1
  • 1 possible duplicate of PhantomJS; click an element – Bergi Commented Jan 27, 2014 at 11:02
Add a ment  | 

2 Answers 2

Reset to default 6

You are attempting to use the click() method on an <a> element which is not supported in all browser by default. Instead try using something like this where instead of myLink.click(); you will have to do eventFire(myLink, 'click');.

Or include jQuery and do something like:

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

评论列表(0)

  1. 暂无评论