2014-11-23 68 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