0

How do i switch to a iframe with selenium, i'm trying to scrap data, but today the website updated and add a iframe. Now i didnt know how to switch and enter in the iframe to collect the data.

thats the iframe =

<iframe _ngcontent-xyi-c154="" type="text/html" frameborder="0" width="100%" scrolling="auto" src="https://direct.argusmedia.com/integration/price/pricedata" style="height: 2808px;"></iframe>
mr robot
  • 3
  • 2

1 Answers1

0

You must use an XPath to locate the iframe element first

iframe = driver.find_element(By.XPATH,"//iframe[@src='https://direct.argusmedia.com/integration/price/pricedata']")

Then switch_to the iframe

driver.switch_to.frame(iframe)

To switch back to the Parent Frame you can use the following line of code:

driver.switch_to.parent_frame()

To switch back to the Top Level Browsing Context / Top Window you can use the following line of code:

driver.switch_to.default_content()

//Wait for frame can you try adding a wait for frame

 wait = WebDriverWait(driver, 30)
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[@src='https://direct.argusmedia.com/integration/price/pricedata']')))
Abhay Chaudhary
  • 1,763
  • 1
  • 8
  • 13
  • the error below apears to me: selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //iframe[@src='https://direct.argusmedia.com/integration/price/pricedata'] But i look in the HTLM end there is that element, now i have no idea what can i do – mr robot Apr 02 '23 at 19:09
  • can you try adding a wait for frame, updated above answer, to wait for frame then switch to it – Abhay Chaudhary Apr 02 '23 at 19:16
  • 1
    i saw that i'm looking for the wrong iframe. I just change the "src='direct.argusmedia.com/integration/". Thank you so much! – mr robot Apr 02 '23 at 19:43