如何解决python Selenium无法通过id找到元素的问题-教程

298 阅读1分钟

我知道有很多像这样的问题,但似乎都没有用。

这是我正在使用的代码

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'captcha_input'))
    )
    print("done")
except:
    print("error")

这是HTML

<div id="control_window_2" style="margin: 0px; position: absolute; z-index: 9999; top: 1479px; left: 441px;" class="modal_container">
<table class="modal">
  <caption></caption>
  <tbody><tr><th scope="col"></th></tr>
  <tr>
    <td class="header">
      <a class="close" onclick="Control.Modal.close()"><img src="/img/button/close.gif" alt="close"></a>
      <p>Confirmation</p>
    </td>
  </tr>
  <tr>
    <td style="padding:5px">
                        <form method="post" action="https://de-legalization.tlscontact.com/eg/CAI/action.php" name="ajax_confirm_action" id="ajax_confirm_action" onsubmit="ajaxPostForm(this,&quot;multiconfirm&quot;,true
                , {onFailure: function(r){ refresh_captcha($$(&quot;.acaptcha&quot;).first()); return false;}}                        ); return false;">
                book_appointment_popup        <input type="hidden" name="f_id" id="f_id" value="">
<input type="hidden" name="fg_id" id="fg_id" value="31499">
<input type="hidden" name="what" id="what" value="book_appointment">
<input type="hidden" name="result" id="result" value="2021-08-26 10:30">
<input type="hidden" name="as_u_id" id="as_u_id" value="">
<input type="hidden" name="_sid" id="_sid" value="fbf909b1eee8e01ed23c64e668776470f7e9214c5e58444df95f49aca1218a90d811e796bc20d8eac7647554218190ee315bf1924c04a9d3e64c71eb613a1bc2">
<input type="hidden" name="timestamp" id="timestamp" value="1629093887.7413">
<input type="hidden" name="skey" id="skey" value="f6bd6daf4a3c70cd380839fb2b22d37cc2537e0c67b97e70b46ada2f220b482e5558e8158ac4bccec7a0e824f3f4981213e80ffdfe55e0cbe33a5df320936682">        
                <div class="action-captcha">
        <img id="action_captcha" src="URL" alt="CAPTCHA code" style="vertical-align:middle;">
        <a class="acaptcha" onclick="refresh_captcha(this); return false;" style="padding-right:15px;"><img src="/img/icon/refresh.png" alt="captcha" width="18" height="18"></a>
        <input name="captcha_input" value="" placeholder="Please input the chars in left captcha" size="30" style="height:34px; padding:6px 12px; font-size:14px; line-height:1.428; color:#666; border:1px solid #ccc; border-radius:4px; box-shadow:inset 0 1px rgba(0,0,0,.075);">
        </div>
        
        <div style="margin-top:15px;text-align:center;" id="parent">
          <input type="submit" class="mt-2 btn btn-primary btn-xl-tls px-5 py-0" id="ajaxConfirmCall_submit" value="Confirm" style="font-weight:bold">
             <input type="button" class="mt-2 btn btn-primary btn-xl-tls px-5 py-0" value="Cancel" onclick="Control.Modal.close()" style="font-weight:bold"><input id="pck" type="hidden" name="pta_confirm_key" value="normal_confirm">        </div>
      </form>
    </td>
  </tr>
  
</tbody></table>
</div>

页面中有一个iframe,但我要找的元素不在里面,但我还是试了一下。

try:
    iframe = driver.find_element_by_xpath("/html/body/iframe")
    driver.switch_to.frame(iframe)
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'captcha_input'))
    )
    print("done")
except:
    print("error")

两者的输出结果都是 "错误"。 有什么想法吗?

在此阅读更多信息:https://stackoverflow.com/questions/68799698/selenium-cant-find-element-by-id-python