Python Selenium switches to iframe in iframe

I'm trying to access iframe in iframe using Selenium, Python and BS4

from bs4 import BeautifulSoup from selenium import webdriver import time import html5lib driver = webdriver.Firefox() driver.implicitly_wait(10) driver.get('http://myurl.com') try: time.sleep(4) iframe = driver.find_elements_by_tag_name('iframe')[0] driver.switch_to_default_content() driver.switch_to_frame(iframe) driver.switch_to_default_content() driver.find_elements_by_tag_name('iframe')[0] output = driver.page_source print output finally: driver.quit(); 

Two more iframes appear in the returned text. How can I access them? I tried in the code above without success.

Any help is appreciated.

+6
source share
1 answer

switch_to_default_content() will bring you back to the beginning of the document. What happens is that you switched to the first iframe , returned to the beginning of the document, and then tried to find the second iframe . Selenium cannot find the second iframe because it is inside the first iframe .

If you remove the second switch_to_default_content() , you should be fine:

 iframe = driver.find_elements_by_tag_name('iframe')[0] driver.switch_to_default_content() driver.switch_to_frame(iframe) driver.find_elements_by_tag_name('iframe')[0] 
+16
source

All Articles