-1

I'm having a problem getting html elements placed inside an iframe. I use XPATH and the problem is that when I execute the command it returns an empty element

instead, if I show the element inspector (f12) and run the same command again, it returns the desired result.

enter image description here

Is there a way to fix this without having to open the element inspector (f12)?

enter image description here

this is my code to get the input in my js file

  • How do you try to get it? Is iFrame content loaded when you execute command? – Justinas Sep 26 '22 at 09:45
  • If I execute the command before opening the element inspector (f12) , the content that appears is empty []. It is after opening the inspector when it returns the element correctly – Juan Manuel Vela Pérez Sep 26 '22 at 09:49
  • _HOW_ do you execute it without opening inspector? Via code - show code. Via magic - show magic. – Justinas Sep 26 '22 at 10:53
  • I run it through code. input is null or empty until I open the element inspector. I post my code. – Juan Manuel Vela Pérez Sep 26 '22 at 11:04
  • 1
    Pleas read [How to ask question](https://stackoverflow.com/help/how-to-ask). Include minimal example of what you have and not screenshots of your code. It differs _when_ you execute that code in perspective of iFrame loading/parsing time – Justinas Sep 26 '22 at 11:15

1 Answers1

0

I deleted the old answer because i miss understood.

You need to declare $x in javascript code, $x is implemented in debugger only (F12) from jquery as far as i know.

here's the $x function

const $x = xp => {
    const snapshot = document.evaluate(
        xp, document, null, 
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
    );
    return [...Array(snapshot.snapshotLength)]
        .map((_, i) => snapshot.snapshotItem(i))
    ;
};

I found the code from this post!

Now you can type console.log($x('/html/body/form/table...')) in the javascript code not the console and it will work

hz-rd
  • 11
  • 4
  • Thanks for the answer, maybe I have formulated my question wrong. The problem is not in displaying an element by console. The $x(...) function is already declared in my project's jquery library. The problem is that the item to get is always null or empty if I don't open the item inspector. I need the object to be inside the iframe if I need to open the element inspector. – Juan Manuel Vela Pérez Sep 26 '22 at 10:29
  • Hmm that's weird, Maybe you should try something like binding the load in the iframe then use the $x(), not sure about that one. – hz-rd Sep 26 '22 at 10:43