使用Python中的Selenium与Web元素交互时出错

时间:2014-11-23 03:58:49

标签: python google-chrome selenium selenium-webdriver

Python-3.4.1 32 bit计算机Windows 7 64 bit使用Selenium Google Chrome version 39 CSV。我正在努力在登录后自动从安全网站下载Load unsafe script文件。在Chrome中,在打开此网站时,它会显示一个图标Menu,我们必须手动点击以加载不同的元素在网站上。我必须单击<!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>按钮,打开一个下拉列表,我必须从中单击一个按钮。页面部分的代码是:

&#13;
&#13;
More
&#13;
&#13;
&#13;

我需要操纵上面代码中的button id="ext-gen435"Export to Excel)和span id="ext-gen465"(上面代码中的More)元素并操纵d_menu = driver.find_element_by_id("ext-gen389") 按钮我试过了:

"ext-gen389"

但是id(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)

它给了我:

{{1}}

TypeError是什么意思:无法读取属性&#39;点击&#39;上面显示的跟踪中的null

由于

1 个答案:

答案 0 :(得分:1)

我认为问题可能在于它正在尝试执行完全匹配,但您匹配的文字并不完全匹配。

试试这个:

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