Selenium Web驱动程序按ID查找问题

时间:2015-07-27 19:19:56

标签: python selenium selenium-webdriver automation web-scraping

这是我第一次使用Selenium Web Driver(使用Python),我正在尝试进入搜索框,输入内容并点击提交。

相关的HTML看起来像:

<iframe id="mainFrame" class="" width="100%" height="" frameborder="0" title="Main content frame" onload="initFrame();" scrolling

....

<tr>

    <td style="vertical-align:middle;">
         <input id="terms" type="text" onblur="RevertText(this,'Enter Search Terms');" onfocus="AllowType(this,'Enter Search Terms')" value="Enter Search Terms" style="color: rgb(164, 164, 164);"></input>
    </td>
    <td style="vertical-align:middle;">
         <input id="srchButt" class="buttAcdmicOne" type="submit" value="Search"></input>
    </td>
</tr>

我的代码如下所示:

driver.switch_to.frame("mainFrame")
elem = driver.find_element_by_id("terms")
elem.send_keys("Test")

但是这不起作用,我不知道为什么它不是......(可能会有多个带有条件ID的东西?)

编辑:

我收到的错误是:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"terms"}

我收到的新错误是:

selenium.common.exceptions.NoSuchFrameException: Message: no such frame: element is not a frame

&#13;
&#13;
<!DOCTYPE html>

<html xmlns="_http://www.w3.org/1999/xhtml" webdriver="true">

<head></head>

<body onload="init();" style="overflow:auto;">
  <div id="overlay" class="" style="height: 0px;"></div>
  <!--

         For Modal Popup 

        -->
  <div style="width:938px;margin-left:30px;"></div>
  <input id="toolBarStatus" type="hidden" value="expanded" name="toolBarStatus"></input>
  <input id="hotTopictarget" type="hidden" value="" name="hotTopictarget"></input>
  <input id="idxTms" type="hidden" name="idxTms" value=""></input>
  <input id="srchWidgFrm" type="hidden" name="srchWidgFrm"></input>
  <input id="isUseRelChkd" type="hidden" name="isUseRelChkd"></input>
  <input id="actionTkn" type="hidden" name="actionTkn"></input>
  <input id="advSrcs" type="hidden" value="" name="advSrcs"></input>
  <input id="advSrcNms" type="hidden" value="" name="advSrcNms"></input>
  <input id="prevSearchTerms" type="hidden" name="prevSearchTerms"></input>
  <input id="segListAll" type="hidden" value="" name="segListAll"></input>
  <input id="isLoaded" type="hidden" name="isLoaded"></input>
  <input id="defaultSrcs" type="hidden" value="270077,11059,8411,270944" name="defaultSrcs"></input>
  <input id="prevSrcsChkd" type="hidden" name="prevSrcsChkd"></input>
  <input id="srcsChkd" type="hidden" value="270077,11059,8411,270944" name="srcsChkd"></input>
  <input id="noSrcs" type="hidden" value="" name="noSrcs"></input>
  <input id="prevDates" type="hidden" value="" name="prevDates"></input>
  <input id="prevSegs" type="hidden" value="" name="prevSegs"></input>
  <div align="left" style="width:98%;height:25px;"></div>
  <iframe id="mainFrame" class="" width="100%" height="" frameborder="0" title="Main content frame" onload="initFrame();" scrolling="no" src="form_main_search.asp" style="height: 721px;" name="mainFrame">

    #document
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" webdriver="true">

    <head></head>

    <body onunload="cleanUp();" onload="init();initAdvance();parent.resizeIframe();" style="height:98%;">
      <table style="width:100%;height:auto;">
        <tbody>
          <tr>
            <td style="width: 170px;height:100%;"></td>
            <td>
              <!--

                         Main Search Starts 

                        -->
              <div align="left" style="width:668px;height:100%;">
                <!--

                             General Search Starts 

                            -->
                <div id="generalSearch" align="left" style="width:662px;">
                  <form id="searchForm" autocomplete="off" onsubmit="validateSearch(this);return false;" action="" method="get" name="searchForm">

                    <table style="width:100%;height:100%;">
                      <tbody>
                        <tr></tr>
                        <tr>
                          <td style="width:662px;height:51px;" colspan="3">
                            <div id="searchDiv_2">
                              <table align="center" style="width:646px;height:100%;">
                                <tbody>
                                  <tr>
                                    <td style="vertical-align:middle;">
                                      <input id="terms" type="text" onblur="RevertText(this,'Enter Search Terms');" onfocus="AllowType(this,'Enter Search Terms')" value="Enter Search Terms" style="color: rgb(164, 164, 164);"></input>

                                    </td>
                                    <td style="vertical-align:middle;">
                                      <input id="srchButt" class="buttAcdmicOne" type="submit" value="Search"></input>
                                    </td>
                                  </tr>
                                </tbody>
                              </table>
                            </div>
                          </td>
                        </tr>
                        <tr></tr>
                      </tbody>
                    </table>
                  </form>
                </div>
                <!--

                     General Search Ends 

                    -->
                <!--

                     Message Box Starts 

                    -->
                <div id="bannerMessageSpace" style="height: 10px;"></div>
                <div id="bannerMessageCont" style="padding-left: 2px;"></div>
                <!--

                     Message Box Ends 

                    -->
                <!--

                     Hottopics links Starts 

                    -->
                <div id="hotTopics" class="" style="display:block;padding-top:14px;width:662px;height:150px;padding-left:2px;"></div>
                <!--

                     Hottopics links Ends 

                    -->
                <!--

                     SearchWidgets Starts 

                    -->
                <div id="SearchWidgets" style="display:block;padding-top:16px;width:100%;height:325px"></div>
                <!--

                     SearchWidgets Ends 

                    -->
              </div>
              <!--

                 Main Search Ends 

                -->
            </td>
          </tr>
        </tbody>

      </table>
      <!--

Footer Starts

-->

      <div style="margin-bottom: 35px; padding-top: 20px; padding-left: 30px; width: 98%;"></div>

      <!--

Footer Ends

-->

      <!--

 Advance Search Option Starts 

-->

      <div id="advanceDiv" style="display:none;background-color: White;border:1px solid #99999…;top:97px;left:184px;width:646px;height:auto;border-top:0px;"></div>

      <!--

 Advance Search Option Ends 

-->

      <!--

 For Modal Popup Starts 

-->

      <div id="divBackground" class="ModalBackground"></div>

      <!--

 For Modal Popup Ends 

-->

      <!--

 Search By Content Type Starts 

-->

      <div id="addnlSrchForm" style="display:none;overflow: hidden;position:absolute;top:34px;lef…width:570px;background-color:white;border:solid 1px #999999;"></div>

      <!--

 Search By Content Type Ends 

-->

      <div id="loading1" class="loading2"></div>

      <div style="position: absolute; width: 210px; z-index: 10; display: none; background-color: White;"></div>

      <script src="js/commonForm.js" type="text/javascript"></script>

      <script src="js/AS.js" type="text/javascript"></script>

      <script src="js/ASadv.js" type="text/javascript"></script>

      <script src="js/suggestSource.js" type="text/javascript"></script>

      <script type="text/javascript"></script>

      <div id="ui-datepicker-div" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all"></div>

    </body>

    </html>

</html>

</iframe>
<div id="one" class="connectors" style="display:none;"></div>

<script src="js/GAnalytics.js" type="text/javascript"></script>

<script type="text/javascript" src="http://app.ubertags.com/javascripts/ubertags.js"></script>

<div id="ui-datepicker-div" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-helper-hidden-accessible"></div>

<script type="text/javascript"></script>

<!--

 Google Code for Remarketing Tag - ECOMMERCE Accou…

-->

<!--

------------------------------------------------
R…

-->

<script type="text/javascript"></script>

<script src="//www.googleadservices.com/pagead/conversion.js" type="text/javascript"></script>

<iframe width="300" height="13" frameborder="0" scrolling="no" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://googleads.g.doubleclick.net/pagead/viewthroughconvers…&frm=0&url=http%3A//www.lexisnexis.com/hottopics/lnacademic/"
title="Google conversion frame" name="google_conversion_frame"></iframe>

<noscript></noscript>

<script type="text/javascript"></script>

<!--

 Google Code for Remarketing Tag - LEAD GEN Accoun…

-->

<!--

------------------------------------------------
R…

-->

<script type="text/javascript"></script>

<script src="//www.googleadservices.com/pagead/conversion.js" type="text/javascript"></script>

<img width="1" border="0" height="1" src="http://googleads.g.doubleclick.net/pagead/viewthroughconvers…&frm=0&url=http%3A//www.lexisnexis.com/hottopics/lnacademic/" alt=""></img>

<noscript></noscript>

<script type="text/javascript"></script>

<!--

 Google Code for Remarketing Tag 

-->

<!--

------------------------------------------------
R…

-->

<script type="text/javascript"></script>

<script src="//www.googleadservices.com/pagead/conversion.js" type="text/javascript"></script>

<script type="text/javascript"></script>

<!--

 Google Code for Remarketing Tag 

-->

<!--

------------------------------------------------
R…

-->

<script type="text/javascript"></script>

<script src="//www.googleadservices.com/pagead/conversion.js" type="text/javascript"></script>

<script type="text/javascript"></script>

<script type="text/javascript" src="//platform.twitter.com/oct.js"></script>

<img width="1" border="0" height="1" src="http://googleads.g.doubleclick.net/pagead/viewthroughconvers…&frm=0&url=http%3A//www.lexisnexis.com/hottopics/lnacademic/" alt=""></img>

<noscript></noscript>

<noscript></noscript>

<script type="text/javascript"></script>

<script type="text/javascript"></script>

<script type="text/javascript"></script>

<noscript></noscript>

<script type="text/javascript"></script>

<noscript></noscript>

<script type="text/javascript"></script>

<script src="http://7205084.collect.igodigital.com/collect.js" type="text/javascript"></script>

<script src="http://www.bizographics.com/collect/?time=1438024251782&pid=…2Fwww.lexisnexis.com%2Fhottopics%2Flnacademic%2F&ref=&fmt=js" type="text/javascript"></script>

<script type="text/javascript"></script>

<div style="display: none;"></div>

<img width="1" border="0" height="1" src="https://imp2.bizographics.com/l" alt="" style="display: none;"></img>

</body>

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:4)

以下是常见的2个原因(有解决方案),为什么你会得到NoSuchElementException

  • 该元素位于iframe内,您需要在搜索之前切换到该元素:

    driver.switch_to.frame("my_frame_id_or_name")
    elem = driver.find_element_by_id("terms")
    elem.send_keys("Test")
    
  • 执行搜索时元素尚不存在。 Wait for it

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 10)
    terms = wait.until(EC.presence_of_element_located((By.ID, "terms")))
    terms.send_keys("test")
    

或者,即使两种解决方案都应该结合起来:

wait = WebDriverWait(driver, 10)
iframe = wait.until(EC.presence_of_element_located((By.ID, "mainFrame")))
driver.switch_to.frame(iframe)

terms = wait.until(EC.visibility_of_element_located((By.ID, "terms")))
terms.send_keys("test")