fx86f
fx86f

Reputation: 69

Selenium element can not be found within iframe

I am trying to retrieve a JSON element, the problem is that in the source code it doesn't exist, but I can find it via inspect element.

I have tried with

C.driver.findElement(By.id("ticket-parsed"))

and via XPath

C.driver.findElement(By.xpath("//*[@id=\"ticket_parsed\"]"));

and I can't find it.

Also

C.driver.switchTo().frame("html5-frame"); 
System.out.println(C.driver.findElement(By.id("ticket_parsed"))); 
C.driver.switchTo().defaultContent();

i get

[[ChromeDriver: chrome on XP (1f75e50635f9dd5b9535a149a027a447)] -> id: ticket_parsed]  

on

 driver.switchTo().frame(0) or driver.switchTo().frame(1) 

i get that the frame doesn't exists

and at last i tried

 WebElement frame = C.driver.findElement(By.id("html5-frame"));
 C.driver.switchTo().frame(frame.getAttribute("ticket_parsed"));

an i got a null pointer exception

Here's an image of the source:

enter image description here

what am I doing wrong?

Upvotes: 0

Views: 8834

Answers (2)

fx86f
fx86f

Reputation: 69

I didn't found a solution with my excising setup,but i did found a js command which gets the object correctly

 document.getElementById("html5-frame").contentDocument.getElementById("ticket_parsed")

you can integrate js commands like this

 JavascriptExecutor js=(JavascriptExecutor)driver;  
 js.executeScript(*yourCommandHere*);

if you want to get the output of the command just add the word return before your command (in this specific situation it didn't work but in any other situation it did)

 *TypeOfData* foo = js.executeScript(return *yourCommandHere*);

at last because of limited time i had to use unorthodox methods like taking screenshots and comparing the images if they are exactly the same

Thanks for the help

Upvotes: 0

Aravin
Aravin

Reputation: 7097

Well!

The element #ticket-parsed is in iFrame. So, you can click it without getting into an iframe.

Here is the code to switch to iFrame,

driver.switchTo().frame("frame_name");

or

driver.switchTo().frame(frame_index);

In your case,

driver.switchTo().frame("html5-frame");

After switching into the iframe, you can click that element using either XPath or CSS.

C.driver.findElement(By.id("ticket-parsed"))

NOTE: After completing the operation inside the iframe, you have to again return back to the main window using the following command.

driver.switchTo().defaultContent();

Upvotes: 4

Related Questions