Skip to content Skip to sidebar Skip to footer

Switching Into Second Iframe In Selenium Python3

I am trying to switch into second iframe of the website for personal auto-filler for my business. Just in case I might get marked as dup, I tried Python Selenium switch into an ifr

Solution 1:

You need to deal with your frames separately: When you finished working with one of them and want to switch to another -- you need to do:

driver.switch_to.default_content()

then switch to another one.

Better to use explicit wait for switching to the frame:

from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


ui.WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "#contants>iframe")))

wherein By you can use any locator.

Solution 2:

As per your html structure, both the iframes are different and the second one is not in between the first one. It would have been inside the first one if the html structure was like:

<div><iframe1><iframe2></iframe2></iframe1></div>

But as your first <div> and first <iframe> are ending before starting the second div and iframe, it means both the iframes are seperate. So, according to your requirements, you just need to switch to the second iframe which can be done using:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.CSS_SELECTOR,"second iframe css"))

Updated Answer: Try the code:

driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="the_iframe"]'))
driver.WAIT
driver.switch_to.frame(driver.find_element_by_xpath('//iframe[contains(@src,'mysafind')]'))
driver.WAIT

Post a Comment for "Switching Into Second Iframe In Selenium Python3"