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 iframe within an iframe already and sadly got not much out of it.
Here are html codes for two iframes. Second iframe is within the first one:
<div id="tab2_1" class="cms_tab_content1" style="display: block;">
<iframe id="the_iframe"src=
"http://www.scourt.go.kr/portal/information/events/search/search.jsp">
</iframe> <!-- allowfullscreen --></div>
<div id="contants">
<iframe frameborder="0" width="100%" height="1100" marginheight="0"
marginwidth="0" scrolling="auto" title="나의 사건검색"
src="http://safind.scourt.go.kr/sf/mysafind.jsp
sch_sa_gbn=&sch_bub_nm=&sa_year=&sa_gubun=&sa_serial=&x=&
y=&saveCookie="></iframe>
<noframes title="나의사건검색(새창)">
<a href="http://safind.scourt.go.kr/sf/mysafind.jspsch_sa_gbn
=&sch_bub_nm=&sa_year=&sa_gubun=&sa_serial=&x=&y=&
saveCookie=" target="_blank"
title="나의사건검색(새창)">프레임이 보이지 않을경우 이곳을 클릭해주세요</a></noframes></div>
Just for a reference- so far, I tried these:
#METHOD-1
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.ID,"the_iframe"))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.CSS_SELECTOR,"#contants > iframe"))
#METHOD-2
driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="the_iframe"]'))
driver.WAIT
driver.switch_to.frame(driver.find_element_by_css_selector('//*[@id="contants"]/iframe'))
driver.WAIT
#METHOD-3
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]
Here is the entire code I have right now:
import time
import requests
from bs4 import BeautifulSoup as SOUP
import lxml
import re
import pandas as pd
import numpy as np
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.ui import Select
import psycopg2
#ID and Password for autologin
usernameStr= 'personalinfo'
passwordStr= 'personalinfo'
driver = webdriver.Chrome('./chromedriver')
ultrawait = WebDriverWait(driver, 9999)
ConsumerName=""
COURTNO=""
CASENO=""
CaseYear=""
CaseBun=""
CaseSerial=""
#AutoLogin
driver.get("http://PERSONALINFO")
username = driver.find_element_by_name('userID')
username.send_keys(usernameStr)
userpassword = driver.find_element_by_name('userPassword')
userpassword.send_keys(passwordStr)
login_button=driver.find_elements_by_xpath("/html/body/div/div[2]/div/form/input")[0]
login_button.click()
#Triggered when str of URL includes words in look_for
def condition(driver):
look_for = ("SangDamPom", "jinHaengNo")
url = driver.current_url
WAIT = WebDriverWait(driver, 2)
for s in look_for:
if url.find(s) != -1:
url = driver.current_url
html = requests.get(url)
soup = SOUP(html.text, 'html.parser')
soup = str(soup)
#Some info crawled
CN_first_index = soup.find('type="text" value="')
CN_last_index = soup.find('"/></td>\n<t')
ConsumerName=soup[CN_first_index+19:CN_last_index]
ConsumerName.replace(" ","")
#Some info crawled
CTN_first_index = soup.find('background-color:#f8f8f8;')
CTN_last_index = soup.find('</td>\n<td height="24"')
COURTNO = soup[CTN_first_index+30:CTN_last_index]
COURTNO = COURTNO.replace('\t', '')
#Some info crawled
CAN_first_index = soup.find('가능하게 할것(현제는 적용않됨)">')
CAN_last_index = soup.find('</a></td>\n<td height="24"')
CASENO=soup[CAN_first_index+19:CAN_last_index]
CaseYear=CASENO[:4]
CaseBun=CASENO[4:-5]
CaseSerial=CASENO[-5:]
print(ConsumerName, COURTNO, CaseYear, CaseBun, CaseSerial)
#I need to press this button for iframe I need to appear.
frame_button=driver.find_elements_by_xpath("//*[@id='aside']/fieldset/ul/li[2]")[0]
frame_button.click()
time.sleep(1)
#Switch iframe
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')]"))
time.sleep(1)
#Insert instit.Name
CTNselect = Select(driver.find_element_by_css_selector('#sch_bub_nm'))
CTNselect.select_by_value(COURTNO)
#Insert Year
CYselect = Select(driver.find_element_by_css_selector('#sel_sa_year'))
CYselect.select_by_value(CaseYear)
#Insert Number
CBselect = Select(driver.find_element_by_css_selector('#sa_gubun'))
CBselect.select_by_visible_text(CaseBun)
#사건번호 입력 (숫자부분)
CS_Insert = WAIT.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#sa_serial")))
CS_Insert.click()
CS_Insert.clear()
CS_Insert.send_keys(CaseSerial)
#Insert Name
CN_Insert = WAIT.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#ds_nm")))
CN_Insert.click()
CN_Insert.clear()
CN_Insert.send_keys(ConsumerName)
break
ultrawait.until(condition)
Don't mind indention errors, problem of copy-paste.
I think its from #Switch iframe I have issue with.
Those inputs that come after #Switch iframe are all functional. I've tested them by opening iframe at another tab.