使用 Selenium 从框架下的嵌套 HTML 代码中识别元素(确认没有 iframe)

Identifying element from nested HTML code under frame using Selenium (confirmed no iframe)

我正在使用 python 学习 Selenium,并试图在某些网站中自动填写表单。

但是,我无法识别框架下的任何元素,这给了我 NoSuchElementException 错误。我尝试使用不同的 find_element 函数,例如CSS、XPATH(非常确定 XPATH 是正确的,因为我从 Firebug 复制了它)并且还尝试使用 WebDriverWait 包含显式等待。然而,什么也没有发生。

我之前试过的命令:

driver.find_element_by_id("testFormNumForLastAttempt"); 
driver.find_element_by_xpath("//input[@type='text' and @name='testFormNumForLastAttempt']");

wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.ID, "testFormNumForLastAttempt")));

我正在尝试切换到另一个框架

    driver.switch_to.frame("main")
        raise exception_class(message, screen, stacktrace)
    NoSuchFrameException: Message: No frame found

我提取了下面的相关 HTML 代码,并正在寻找识别 testFormNumForLastAttempt 输入参数的解决方案。

提前致谢。任何帮助将不胜感激。

<html>

<frameset rows="0,0,*" frameborder="no" border="0">

  <frame name="applet_container" scrolling="no" marginwidth="0" marginheight="0" noresize src="">
    <frame name="javascript_container" scrolling="no" marginwidth="0" marginheight="0" noresize src="/repoes/td-es-app517/RETJSContainer_WOC.do?__AUTHENTICATION_REQUEST_PARAMETER_MECHANISM__=applet">
      <frame name="main" scrolling="yes" noresize src="/repoes/td-es-app517/Loading.do" marginwidth="0" marginheight="0" frameborder="0">

        <html>

        <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <meta http-equiv="Cache-Control" content="no-cache">
          <meta http-equiv="Expires" content="0">
          <meta http-equiv="Pragma" content="no-cache">


          <div id="authByTestForm">
            <table width="100%" cellspacing="0" cellpadding="0" border="0">
              <tbody>
                <tr>
                  <td>Test Form No. for Last Attempt <span class="star">*</span> </td>
                  <td>
                    <input type="text" size="10" maxlength="8" name="testFormNumForLastAttempt" id="testFormNumForLastAttempt" value="" class="uppercase" autocomplete="off">

                  </td>
                </tr>
                <tr>
                  <td valign="top">Date of Birth <span class="star">*</span></td>
                  <td>
                    <table class="example_table">
                      <tbody>
                        <tr>
                          <td colspan="7">
                            <input type="text" size="4" maxlength="4" id="testFormBirthYear" name="testFormBirthYear" onkeyup="moveOnMax(this,'testFormBirthMonth')" value="" autocomplete="off"> Year

                            <input type="text" size="2" maxlength="2" id="testFormBirthMonth" name="testFormBirthMonth" onkeyup="moveOnMax(this,'testFormBirthDay')" value="" autocomplete="off"> Month

                            <input type="text" size="2" maxlength="2" id="testFormBirthDay" name="testFormBirthDay" value="" autocomplete="off"> Day

                          </td>
                        </tr>
                        <tr>
                          <td>Example:</td>
                          <td class="example_cell">1995</td>
                          <td>Year</td>
                          <td class="example_cell">09</td>
                          <td>Month</td>
                          <td class="example_cell">08</td>
                          <td>Day</td>
                        </tr>
                      </tbody>
                    </table>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>

带有 id 作为 testFormNumForLastAttempt<input> 标签似乎嵌套在 <frame>.

的 3 层中

因此,要在元素中 click(),您必须:

  • (安全地)忽略 parentchild <frameset> 标签的存在。
  • frame_to_be_available_and_switch_to_it()的第一层引入WebDriverWait
  • 为第二层_frame_to_be_available_and_switch_to_it().
  • 引入WebDriverWait
  • frame_to_be_available_and_switch_to_it()的第三层引入WebDriverWait
  • 为所需的 element_to_be_clickable().
  • 引入 WebDriverWait
  • 您可以使用以下解决方案:

    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.common.by import By
      
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"applet_container")))
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[name='javascript_container'][src$='__AUTHENTICATION_REQUEST_PARAMETER_MECHANISM__=applet']")))
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[@name='main' and contains(@src, 'Loading')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#testFormNumForLastAttempt[name='testFormNumForLastAttempt']"))).click()
      

You can find a relevant discussion in


结尾