2014-11-23 67 views
0

我有Python-3.4.1 32 bitWindows 7 64 bit機器上Selenium使用Google Chrome version 39。我試圖在登錄後自動從安全網站上下載一個CSV文件。在Chrome中,打開此網站時,它會顯示一個圖標Load unsafe script,我們必須手動點擊才能加載網站上的不同元素。我必須點擊一個Menu按鈕,打開一個下拉列表,我必須點擊一個按鈕。對於頁面的部分中的代碼是:與使用Python中的Web元素交互的錯誤

<!DOCTYPE html> 
 
    <html id="ext-gen3" class=" ext-strict x-viewport" decorator="blank"> 
 
     <head></head> 
 
     <body id="ext-gen4" class="ext-gecko3 ext-linux cwc-view x-border-layout-ct" onload="cwc.getFrameworkWindow()"> 
 
      <div id="cwc_header" class=" masthead x-border-panel" style="left: 0px; top: 0px; width: 1366px; height: 27px;"></div> 
 
      <div id="cwcNavPanel" class=" x-panel cwc-navPanel x-border-panel" style="width: 220px; left: 0px; top: 56px;"></div> 
 
      <div id="cwcCenterPanel" class=" x-tab-panel cwc-centerPanel x-border-panel" style="left: 228px; top: 35px; width: 1138px;"> 
 
       <a id="mainContent"></a> 
 
       <div id="ext-gen55" class="x-tab-panel-header x-unselectable x-tab-panel-header-plain" style="width: 1138px;"></div> 
 
       <div id="ext-gen56" class="x-tab-panel-bwrap"> 
 
        <div id="ext-gen57" class="x-tab-panel-body x-tab-panel-body-top" style="width: 1138px; height: 331px;"> 
 
         <div id="ext-comp-943917" class=" x-panel x-panel-noborder x-hide-nosize" style="width: 1136px;"></div> 
 
         <div id="ext-comp-943936" class=" x-panel x-panel-noborder" style="width: 1136px;"> 
 
          <div id="ext-gen365" class="x-panel-bwrap"> 
 
           <div id="ext-gen366" class="x-panel-body x-panel-body-noheader x-panel-body-noborder" style="width: 1136px; height: 330px;"> 
 
            <div id="mif-comp-943937" class=" x-panel x-panel-noborder" style="width: 1136px;"> 
 
             <div id="ext-gen368" class="x-panel-bwrap"> 
 
              <div id="ext-gen369" class="x-panel-tbar x-panel-tbar-noheader x-panel-tbar-noborder" style="width: 1136px;"> 
 
               <div id="ext-comp-943935" class="x-toolbar x-small-editor x-toolbar-layout-ct" style="width: 1136px;"> 
 
                <table class="x-toolbar-ct" cellspacing="0"> 
 
                 <tbody> 
 
                  <tr> 
 
                   <td class="x-toolbar-left" align="left"> 
 
                    <table cellspacing="0"> 
 
                     <tbody> 
 
                      <tr class="x-toolbar-left-row"> 
 
                       <td id="ext-gen422" class="x-toolbar-cell"></td> 
 
                       <td id="ext-gen424" class="x-toolbar-cell"></td> 
 
                       <td id="ext-gen426" class="x-toolbar-cell"></td> 
 
                       <td id="ext-gen428" class="x-toolbar-cell"></td> 
 
                       <td id="ext-gen430" class="x-toolbar-cell"></td> 
 
                       <td id="ext-gen432" class="x-toolbar-cell"></td> 
 
                       <td id="ext-gen434" class="x-toolbar-cell"> 
 
                        <table id="ext-comp-943967" class="x-btn ltr x-btn-noicon x-btn-menu-active" cellspacing="0" style="width: auto;"> 
 
                         <tbody class="x-btn-small x-btn-icon-small-left"> 
 
                          <tr></tr> 
 
                          <tr> 
 
                           <td class="x-btn-ml"></td> 
 
                           <td class="x-btn-mc "> 
 
                            <em id="ext-gen436" class="x-btn-arrow" unselectable="on" role="menu" aria-haspopup="true"> 
 
                             <button id="ext-gen435" class=" x-btn-text" type="button"> 
 

 
                              More 
 

 
                             </button> 
 
                            </em> 
 
                           </td> 
 
                           <td class="x-btn-mr"></td> 
 
                          </tr> 
 
                          <tr></tr> 
 
                         </tbody> 
 
                        </table> 
 
                       </td> 
 
                      </tr> 
 
                     </tbody> 
 
                    </table> 
 
                   </td> 
 
                   <td class="x-toolbar-right" align="right"></td> 
 
                  </tr> 
 
                 </tbody> 
 
                </table> 
 
               </div> 
 
              </div> 
 
              <div id="ext-gen370" class="x-panel-body ux-mif-mask-target x-panel-body-noheader x-panel-body-noborder" style="width: 1136px; height: 303px;"></div> 
 
             </div> 
 
            </div> 
 
           </div> 
 
          </div> 
 
         </div> 
 
        </div> 
 
       </div> 
 
      </div> 
 
      <!-- 
 

 
      Telephony Applet 
 

 
      --> 
 
      <iframe id="ext-gen358" class="ext-shim" frameborder="0" src="/sm/js/9.33.4005/extjs/resources/images/default/s.gif" style="display: block; visibility: visible; z-index: 14998; left: 617px; top: 84px; width: 170px; height: 246px;"></iframe> 
 
      <div id="ext-comp-943902" class="x-tip" style="position: absolute; z-index: 20002; visibility: hidden; width: 76px; left: 666px; top: 86px; display: none;"></div> 
 
      <div id="cwcNavPanel-xsplit" class="x-layout-split x-layout-split-west x-unselectable x-splitbar-h" style="left: 220px; top: 56px; height: 332px;"></div> 
 
      <div id="ext-gen54" class=" x-unselectable x-splitbar-proxy x-splitbar-proxy-h"></div> 
 
      <div id="ext-gen359" style="position: absolute; left: -1000px; top: -1000px; visibility:…; text-transform: none; letter-spacing: normal; width: auto;"></div> 
 
      <div id="ext-gen76" class="x-shadow" style="z-index: 14999; left: 617px; top: 84px; width: 170px; height: 246px; display: block;"></div> 
 
      <div id="ext-comp-943968" class="x-menu x-menu-floating x-layer" style="position: absolute; z-index: 15000; visibility: visible; left: 621px; top: 81px;" role="application"> 
 
       <a id="ext-gen445" class="x-menu-focus" tabindex="-1" onclick="return false;" href="#" role="presentation" aria-labelledby="ext-comp-943968"></a> 
 
       <ul id="ext-gen446" class="x-menu-list" style="height: 244px;" role="menu"> 
 
        <li id="x-menu-el-ext-comp-943969" class="x-menu-list-item" role="presentation"></li> 
 
        <li id="x-menu-el-ext-comp-943970" class="x-menu-list-item" role="presentation"></li> 
 
        <li id="x-menu-el-ext-comp-943971" class="x-menu-list-item" role="presentation"></li> 
 
        <li id="x-menu-el-ext-comp-943972" class="x-menu-list-item" role="presentation"></li> 
 
        <li id="x-menu-el-ext-comp-943973" class="x-menu-list-item" role="presentation"></li> 
 
        <li id="x-menu-el-ext-comp-943974" class="x-menu-list-item" role="presentation"></li> 
 
        <li id="x-menu-el-ext-comp-943975" class="x-menu-list-item" role="presentation"></li> 
 
        <li id="x-menu-el-ext-comp-943976" class="x-menu-list-item" role="presentation"></li> 
 
        <li id="x-menu-el-ext-comp-943977" class="x-menu-list-item x-menu-item-active" role="presentation"> 
 
         <a id="ext-comp-943977" class="x-menu-item x-unselectable" href="#" unselectable="on" hidefocus="true" role="menuitem" tabindex="-1"> 
 
          <img id="ext-gen464" class="x-menu-item-icon " src="/sm/js/9.33.4005/extjs/resources/images/default/s.gif" alt=""></img> 
 
          <span id="ext-gen465" class="x-menu-item-text"> 
 

 
           Export to Excel 
 

 
          </span> 
 
         </a>

我需要操縱Morebutton id="ext-gen435"在上面的代碼),並Export to Excelspan id="ext-gen465"在上面的代碼)元素和操縱More按鈕我嘗試:

d_menu = driver.find_element_by_id("ext-gen389") 

但ID("ext-gen389")不斷變化,所以我嘗試:

d_menu = driver.find_element(By.XPATH, '//button[text()="Menu"]') 
ActionChains(driver).move_to_element(d_menu).click(d_menu).perform() 

但是,這是給我的錯誤:

Traceback (most recent call last): 
    File "C:\Users\arjun_m\Desktop\Head_Bashing\Running_Chrome_HPSM.py", line 72, in <module> 
    d_menu = driver.find_element(By.XPATH, '//button[text()="Menu"]') 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 662, in find_element 
    {'using': by, 'value': value})['value'] 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute 
    self.error_handler.check_response(response) 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.NoSuchElementException: Message: no such element 
    (Session info: chrome=39.0.2171.65) 
    (Driver info: chromedriver=2.12.301325 (962dea43ddd90e7e4224a03fa3c36a421281abb7),platform=Windows NT 6.1 SP1 x86_64) 

如何處理這兩種元素?

我想:

driver.find_element(By.XPATH, '//button[contains(text(),"Menu")]').click() 

,它給我的錯誤:

Traceback (most recent call last): 
    File "<pyshell#4>", line 1, in <module> 
    driver.find_element(By.XPATH, '//button[contains(text(),"Menu")]').click() 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 662, in find_element 
    {'using': by, 'value': value})['value'] 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute 
    self.error_handler.check_response(response) 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.NoSuchElementException: Message: no such element 
    (Session info: chrome=39.0.2171.65) 
    (Driver info: chromedriver=2.12.301325 (962dea43ddd90e7e4224a03fa3c36a421281abb7),platform=Windows NT 6.1 SP1 x86_64) 

我想:

driver.find_element(By.XPATH, '//button[contains(text(),"Menu")]').click() 

和它給我:

Traceback (most recent call last): 
    File "<pyshell#33>", line 1, in <module> 
    driver.find_element(By.XPATH, '//button[contains(text(),"Menu")]').click() 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 662, in find_element 
    {'using': by, 'value': value})['value'] 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute 
    self.error_handler.check_response(response) 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.evaluate threw exception: TypeError: Cannot read property 'click' of null 
    (Session info: chrome=39.0.2171.71) 
    (Driver info: chromedriver=2.12.301325 (962dea43ddd90e7e4224a03fa3c36a421281abb7),platform=Windows NT 6.1 SP1 x86_64) 

這是什麼意思TypeError:無法在上面顯示的跟蹤中讀取屬性'點擊'空

感謝

+0

您是否嘗試過不同的選擇?像'cssSelector'一樣? – Saifur 2014-11-23 04:17:15

+0

不,我沒有。你能解釋一下嗎? – Arun 2014-11-23 07:31:07

回答

1

我認爲這個問題可能是它試圖進行精確匹配,但你匹配的文本不完全匹配。

試試這個:

driver.find_element(By.XPATH, '//button[contains(text(),"Menu")]').click() 
driver.find_element(By.XPATH, '//span[contains(text(),"Export to Excel")]').click() 
+0

我認爲是這樣。不完全匹配 – Saifur 2014-11-23 04:24:47

+0

@Richard,我試過你的代碼,它給了我一個錯誤。我已將它們包含在我的帖子中。請指教! – Arun 2014-11-23 07:36:00

+0

@Arun - 典型的'NoSuchElement'異常可能由兩件事引起。元素是在'iframe'中(我不認爲是這種情況),或者加載速度很慢。 – Richard 2014-11-24 17:30:34