Skip to content Skip to sidebar Skip to footer

Phantomjs Not Mimicking Browser Behavior When Looking At Youtube Videos

I posted this question to the PhantomJS mailing list a week ago, but have gotten no response. Hoping for better luck here... I've been trying to use PhantomJS to scrape informatio

Solution 1:

Patrick's answer got me on the right track, but the full story is as follows.

Youtube's Javascript probes the browser's capabilities before deciding whether to create some kind of video element. After trawling through the minified code, I was eventually able to fool Youtube into thinking PhantomJS supported HTML5 video by wrapping document.createElement in the page's onInitialized callback.

page.onInitialized = function () {
  page.evaluate(function () {
    var create = document.createElement;
    document.createElement = function (tag) {
      var elem = create.call(document, tag);
      if (tag === "video") {
        elem.canPlayType = function () { return"probably" };
      }
      return elem;
    };
  });
};

However, this was a misstep; to get the <embed> tag I was originally after, I needed to make Youtube's code think PhantomJS supports Flash, not HTML5 video. That's also doable:

page.onInitialized = function () {
  page.evaluate(function () {
    window.navigator = {
      plugins: { "Shockwave Flash": { description: "Shockwave Flash 11.2 e202" } },
      mimeTypes: { "application/x-shockwave-flash": { enabledPlugin: true } }
    };
  });
};

So that's how it's done.

Solution 2:

Solution 3:

As on option - try to build phantomjs with video/audio support by yourself.

Original answer link: https://github.com/ariya/phantomjs/issues/10839#issuecomment-331457673

Post a Comment for "Phantomjs Not Mimicking Browser Behavior When Looking At Youtube Videos"