0

In any YouTube video page (Gangnam Style for example), some elements can't be accessed by their XPath. From example, I'm trying to access the "Show more" button

enter image description here

by getting its XPath from the Inspect Window

enter image description here

and using this code

btn=$x('//*[@id="widget_bounds"]/div[2]/div[4]/div[7]/div[3]/span[1]');

but I get nothing, or more precisely, an empty list:

enter image description here

I've never encountered this problem before, are they using some sort of obfuscation trickery to prevent the item from being accessed?

Is there a way to work around it?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • 1
    are sure there's not a frame in there somewhere? google loves them... – dandavis Feb 20 '14 at 20:00
  • No idea, I'm a C++ programmer just dabbling in JavaScript, I'm not even sure what you mean :) – sashoalm Feb 20 '14 at 20:01
  • Did you try it in pieces and see where it breaks? – epascarello Feb 20 '14 at 20:02
  • 2
    i mean you can't reach it from the top frame using xpath, you need to start at the nearest contentWindow, if the domain controls allow it. I just checked, and that is indeed your problem. – dandavis Feb 20 '14 at 20:02
  • Is it on purpose? For obfuscation I mean? Another thing I tried was getting a list of all elements using `getElementsByTagName('*')`, and search through them, but that didn't work either. – sashoalm Feb 20 '14 at 20:06
  • Has nothing to do with obfuscation, it has to do with the fact it is not in the same window context. – epascarello Feb 20 '14 at 20:12
  • @dandavis Aha, I think I understood the problem, there indeed is an iframe, and above it XPath works, but the XPath for anything inside its subtree doesn't. So how do I access elements inside the subtree? – sashoalm Feb 20 '14 at 20:18
  • possible duplicate of [Is there a way to change context to iframe in javascript console?](http://stackoverflow.com/questions/7961229/is-there-a-way-to-change-context-to-iframe-in-javascript-console) – sashoalm Feb 20 '14 at 20:24

1 Answers1

1

The content you want to reach is in an iframe on the page. BUT the problem is you will not be able to get to the content because the Same Origin Policy is going to prevent it.

document.getElementById("I0_1392927253257").contentWindow.document

SecurityError: Blocked a frame with origin "http://www.youtube.com" from accessing a cross-origin frame.

Check out @sashoalm's link below to change the context of iframe in Chrome.

Community
  • 1
  • 1
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Wait, does that mean that anything inside an iframe is inaccessible from the JavaScript console? – sashoalm Feb 20 '14 at 20:19
  • Well the console is able to access it, but you are not going to be able to run JavaScript code on that frame. You would need to fetch the source of the iframe and open that up in a new window and run the code there. – epascarello Feb 20 '14 at 20:22
  • Actually, turns out there is a question about that - [Is there a way to change context to iframe in javascript console?](http://stackoverflow.com/questions/7961229/is-there-a-way-to-change-context-to-iframe-in-javascript-console) – sashoalm Feb 20 '14 at 20:30