使用 Selenium 访问嵌套的 iframe

Access to nested iframes with Selenium

我有这个网页,其中包含以下 HTML 代码:

我试过了。 WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.iframeTGR"))) WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"busqueda"))) WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//输入[@value='Aceptar'][@class='boton'] "))).点击()

但是消息:过时的元素引用:元素未附加到页面文档

我需要访问发件人的字段并单击表单底部的输入。

<section class="text-center" style="padding: 0"> <!-- class="d-block d-sm-none" style="padding: 0 10px;" -->
<div style="z-index: 0;">
    <iframe class="embed-responsive-item iframeTGR" src="https://www.tesoreria.cl/portal/portlets/acceso_usuario/cognito/CognitoController.jpf?vr=MzAzM2NhNjM1OGMyMmFkMDRjNjUwMWYxZGM4YjUyOTRhZjhlLDIwMjAtMTAtMDlUMjA6MzQ6MTFaLDc5OTA3NzkwLHYydUIzUHM1YkpxV0RSanQ3NUV3ZEIzTStDUUhvbDN4LHIxMCxDbGF2ZVRyaWJ1dGFyaWE=" style="border-width: 0px !important; min-height: 600px !important; width: 100% !important;">
        <html>
            <head>
                <link href="/portal/resources/css/tesoreria.css" rel="stylesheet" type="text/css">
                <link href="/portal/resources/css/tgrWp.css" rel="stylesheet" type="text/css">
            </head>
            <body>
                <iframe src="redireccion.jsp?redireccion=r10" name="busqueda" id="id_busqueda" class="tgr_wp_id_busqueda" scrolling="auto" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0">
                    <html lang="es"><head><link rel="stylesheet" href="/certificaWeb/resources/css/custom-theme/jquery-ui-1.10.4.custom.css">  
                        <link rel="stylesheet" href="/certificaWeb/resources/css/custom-theme/default.css">  
                        <link rel="stylesheet" href="/certificaWeb/resources/css/cssTGRlocal.css">
                                <title>
                                    Comprobantes de Pago
                                </title>
                                <meta name="modificado" content="29-08-2012 14:52, by mce">
                            </head>
                            <body marginwidth="0" marginheight="0">
                            
                           
                        
                            
                        <table align="center"> 
                          <tbody><tr> 
                            <td align="center"> <table class="tblAnchoMM tblForm"> 
                                <caption>
                                 Consulta Comprobante de pago 
                                </caption> 
                                <tbody><tr> 
                                  <td> <!--inicio tabla principal--> 
                                    <form action="/certificaWeb/comprobantes/pagos/doConsulta.do" method="post"> <br>        
                                     <br> 
                                    <table align="center" class="tblForm tblAnchoSS"> 
                                      <tbody><tr> 
                                        <th colspan="2"> Por favor ingresar el Rut del Contribuyente</th> 
                                      </tr> 
                                      <tr> 
                                        <td class="tblFormTxt" width="60%">Tipo de Contribuyente </td> 
                                        <td> <input type="hidden" name="wlw-select_key:{actionForm.tipo}OldValue" value="true">
                                        <select name="wlw-select_key:{actionForm.tipo}" id="tipoCont" accesskey="s" onchange="chTipo(this.value)">
                                        <option value="2">ROL</option>
                                        <option value="1" selected="">RUT</option></select></td> 
                                      </tr> 
                                      <tr id="tipoRol" style="display: none;"> 
                                        <td class="tblFormTxt">Comuna / Rol / SubRol </td> 
                                        <td><input type="text" name="{actionForm.rolComuna}" id="comuna" class="formularios" maxlength="3">
                                                        <input type="text" name="{actionForm.rolRol}" id="rol" class="formularios" maxlength="5">
                                                        <input type="text" name="{actionForm.rolSubrol}" id="subrol" class="formularios" maxlength="3"> </td> 
                                      </tr> 
                                      <tr id="tipoRut" style="display: block;"> 
                                        <td class="tblFormTxt">Rut </td>
                                     <td> 
                                             <input type="text" name="{actionForm.rut}" id="rut" maxlength="8" size="8" disabled="" readonly="" onkeyup="{solonumeros(this,this.value.charAt(this.value.length-1))}"> - <input type="text" name="{actionForm.dv}" id="dv" class="formularios" maxlength="1" size="1" disabled="" readonly="" style="width: 15px;">
                                          </td>       
                                      </tr> 
                                      <tr> 
                                        <td class="tblFormTxt">Numero de Formulario 
                                                        </td> 
                                        <td> <input type="text" name="{actionForm.formNum}" id="formulario" maxlength="5" onkeyup="{solonumeros(this,this.value.charAt(this.value.length-1))}"></td> 
                                      </tr> 
                                      <tr> 
                                        <td class="tblFormTxt">Folio de Formulario </td>
                                        <td> <input type="text" name="{actionForm.formFolio}" id="folio" maxlength="10" onkeyup="{solonumeros(this,this.value.charAt(this.value.length-1))}"></td> 
                                      </tr> 
                                      <tr> 
                                        <td class="tblFormTxt">Fecha desde <label style="color:#456789; font-size:70%;">(DD-MM-AAAA)</label></td>
                                       <td> <input type="text" name="{actionForm.formDesde}" id="formDesde" value="01-10-2020" maxlength="10" style="width: 80px; height: 20px;">
                                         <!--   <td> <input type="text" id="desde" name="L15" size="10"  class="classFecha2" align="middle" style="background:#cccccc" readonly>-->
                                        </td> 
                                       <!-- <td  class="tblFormTxt" width="37%" style="color:#456789; font-size:70%;">(aaaa-mm-dd)</td>-->
                                      </tr> 
                                      <tr> 
                                        <td class="tblFormTxt">Fecha hasta <label style="color:#456789; font-size:70%;">(DD-MM-AAAA)</label></td>  
                                          <td> <input type="text" name="{actionForm.formHasta}" id="formHasta" value="09-10-2020" maxlength="10" style="width: 80px; height: 20px;">
                                          <!--   <td> <input type="text" id="hasta" name="L15" size="10" class="classFecha2" align="middle" style="background:#cccccc" readonly> -->
                                        </td>
                                        <!-- <td  class="tblFormTxt" width="37%" style="color:#456789; font-size:70%;">(aaaa-mm-dd)</td>-->
                                      </tr> 
                                    </tbody></table> 
                                    <br> 
                                    <input type="hidden" name="rutContribuyente" id="rutContribuyente" value="79907790">
                                    <input type="hidden" name="dvContribuyente" id="dvContribuyente" value="6">
                        
                        
                        <!--<input type="hidden" name="almacenista"  id="almacenista" value="<!%=almacenista%>"> -->
                                    
                                    <div align="center"> <input type="submit" class="boton" value="Aceptar">   </div> 
                                    </form>
                                     </td> 
                                  <!--fin tabla principal--> 
                                </tr> 
                            </tbody></table></td> 
                          </tr> 
                        </tbody>
                    </table>    
                       
                    </body>
                    </html>
                </iframe>
            </body>
        </html>
    </iframe> <!-- https://www.tesoreria.cl/LoginPortalCognito/login?go=rpl6_w81 -->
</div>

要访问嵌套 iframe,您需要同时切换到 iframes 才能访问输入元素。

使用 WebDriverWait() 并等待 frame_to_be_available_and_switch_to_it()

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,".embed-responsive-item.iframeTGR")))
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"id_busqueda")))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[value='Aceptar']"))).click()

您需要导入以下库

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