提问者:小点点

在Selenium Python3中切换到第二个iframe[重复]


我正在尝试切换到网站的第二个iframe,用于我的业务的个人自动填充。

以防万一,我可能会被标记为dup,我尝试Python硒切换到iframe内的iframe已经和可悲的是没有得到太多。

这是两个iframe的html代码。第二个iframe在第一个iframe中:

<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=&amp;sch_bub_nm=&amp;sa_year=&amp;sa_gubun=&amp;sa_serial=&amp;x=&amp;
y=&amp;saveCookie="></iframe>

<noframes title="나의사건검색(새창)">
<a href="http://safind.scourt.go.kr/sf/mysafind.jspsch_sa_gbn
=&amp;sch_bub_nm=&amp;sa_year=&amp;sa_gubun=&amp;sa_serial=&amp;x=&amp;y=&amp;
saveCookie=" target="_blank"
title="나의사건검색(새창)">프레임이 보이지 않을경우 이곳을 클릭해주세요</a></noframes></div>

仅供参考-到目前为止,我尝试了这些:

#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]

这是我现在拥有的整个代码:

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)

不要介意缩进错误,复制粘贴的问题。

我认为它来自#Switch iframe,我有问题。

#Switch iframe之后的那些输入都是功能性的。我通过在另一个选项卡上打开iframe来测试它们。


共2个答案

匿名用户

您需要单独处理您的框架:当您完成其中一个框架的工作并想要切换到另一个框架时-您需要执行以下操作:

driver.switch_to.default_content()

然后切换到另一个。

最好使用显式等待切换到帧:

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")))

其中通过您可以使用任何定位器。

匿名用户

根据您的html结构,两个iframe都不同,第二个不在第一个之间。如果html结构如下所示,它将在第一个内部:

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

但作为你的第一个<代码>

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

更新答案:
试试代码:

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