无法使用selenium webdriver点击网页中的链接

时间:2016-05-31 18:16:06

标签: python selenium xpath selenium-webdriver

我想使用selenium python webdriver点击路由器网页中的“无线”链接。下面是html:

  <html>
       <head>
       <frameset framespacing="0" frameborder="0" border="0" rows="50,*,15">
       <frame noresize="" marginheight="0" marginwidth="0" target="_self" scrolling="no" border="0" frameborder="no" name="logofrm" src="logo.html">
       <frameset border="0" frameborder="0" cols="170,*">
       <frame noresize="" marginheight="0" marginwidth="0" target="_self" scrolling="no" border="0" frameborder="no" name="menufrm" src="menu.html">
       <html>
          <head>
          <body class="mainMenuBody" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">
             <table cellspacing="0" cellpadding="0" border="0" height="100%">
             <tbody>
                <tr>
                   <td width="1" style="background-color: #C35822"></td>
                   <td class="menu" width="170" valign="top" align="left">
                      <br>
                      <script language="javascript">
                      <div id="folder0">
                      <div id="folder1" style="display: block;">
                      <div id="folder2" style="display: none;">
                      <div id="folder3" style="display: none;">
                      <div id="folder4" style="display: none;">
                      <div id="item5" style="display: none;">
                      <div id="item6" style="display: none;">
                      <div id="item7" style="display: none;">
                      <div id="item8" style="display: none;">
                      <div id="folder9" style="display: none;">
                      <div id="folder10" style="display: none;">
                      <div id="folder11" style="display: none;">
                      <div id="folder12" style="display: block;">
                      <div id="folder13" style="display: none;">
                      <div id="item14" style="display: none;">
                      <div id="item15" style="display: none;">
                      <div id="item16" style="display: none;">
                      <div id="folder17" style="display: none;">
                      <div id="item18" style="display: none;">
                      <div id="folder19" style="display: none;">
                      <div id="item20" style="display: none;">
                      <div id="item21" style="display: none;">
                      <div id="item22" style="display: none;">
                      <div id="folder23" style="display: none;">
                      <div id="folder24" style="display: none;">
                      <div id="item25" style="display: none;">
                      <div id="item26" style="display: none;">
                      <div id="folder27" style="display: none;">
                      <div id="folder28" style="display: none;">
                      <div id="folder29" style="display: none;">
                      <div id="folder30" style="display: none;">
                      <div id="folder31" style="display: none;">
                      <div id="folder32" style="display: none;">
                      <div id="item33" style="display: none;">
                      <div id="item34" style="display: none;">
                      <div id="folder35" style="display: none;">
                      <div id="item36" style="display: none;">
                      <div id="item37" style="display: none;">
                      <div id="item38" style="display: none;">
                      <div id="item39" style="display: none;">
                      <div id="folder40" style="display: none;">
                      <div id="item41" style="display: none;">
                      <div id="item42" style="display: none;">
                      <div id="folder43" style="display: none;">
                      <div id="folder44" style="display: none;">
                      <div id="folder45" style="display: none;">
                      <div id="folder46" style="display: none;">
                      <div id="folder47" style="display: none;">
                      <div id="item48" style="display: none;">
                      <div id="item49" style="display: none;">
                      <div id="item50" style="display: none;">
                      <div id="item51" style="display: none;">
                      <div id="folder52" style="display: none;">
                      <div id="folder53" style="display: block;">
                      <table cellspacing="2" cellpadding="0" border="0" width="100%">
                      <tbody>
                      <tr>
                      <td class="menuCell" width="100%" valign="middle" nowrap="">
                      <a class="menuLink" onclick="javascript:clickOnFolder(53)" target="basefrm" href="wlswitchinterface0.wl">   Wireless  </a>
                      </td>
                      </tr>
                      </tbody>
                      </table>
                      </div>
                      <div id="item54" style="display: block;">
                      <div id="item55" style="display: block;">
                      <div id="item56" style="display: block;">
                      <div id="item57" style="display: block;">
                      <div id="item58" style="display: block;">
                      <div id="item59" style="display: block;">
                      <div id="folder60" style="display: block;">
                      <div id="folder61" style="display: block;">
                      <div id="folder62" style="display: none;">
                      <div id="item63" style="display: none;">
                      <div id="item64" style="display: none;">
                      <div id="item65" style="display: none;">
                      <div id="folder66" style="display: none;">
                      <div id="folder67" style="display: none;">
                      <div id="folder68" style="display: none;">
                      <div id="folder69" style="display: none;">
                      <div id="folder70" style="display: none;">
                      <div id="item71" style="display: none;">
                      <div id="item72" style="display: none;">
                      <div id="folder73" style="display: none;">
                      <div id="folder74" style="display: none;">
                      <div id="folder75" style="display: none;">
                      <noscript>You must enable JavaScript in your browser.</noscript>
                      </td>
                      <td width="1" style="background-color: #C35822"></td>
                      <td style="background-color: #ffffff"></td>
                      </tr>
                      </tbody>
                      </table>
                      </body>
                      </html>
                      </frame>
                      <frame noresize="" marginheight="16" marginwidth="0" target="_self" scrolling="auto" border="0" frameborder="no" name="basefrm" src="info.html">
                      </frameset>
                      <frame noresize="" marginheight="0" marginwidth="0" target="_self" scrolling="no" border="0" frameborder="no" name="footerfrm" src="footer.html">
                      </frameset>
                      </html>

我的剧本中有以下行:

driver.find_element_by_xpath('//a[@href="wlswitchinterface0.wl"]').click()

但它不起作用并抛出NoSuchElementException

  

NoSuchElementException:消息:无法找到元素:   { “方法”: “的xpath”, “选择器”: “//一个[@href = \” wlswitchinterface0.wl \ “]”}

2 个答案:

答案 0 :(得分:1)

问题不在于引号,而是因为你在一个框架内。

首先,你需要在他的内部切换:

frame = driver.find_element_by_xpath("//frame[@name='menufrm']")
driver.switch_to_frame(frame)

现在,您可以访问您的链接:

driver.find_element_by_xpath("//a[@href='wlswitchinterface0.wl']").click()

在此框架内访问所需内容后,您需要切换到默认内容:

driver.switch_to_default_content()

答案 1 :(得分:0)

问题似乎是引用。使用以下语法:

 `objMesh.geometry.applyMatrix( new THREE.Matrix4().makeTranslation( 1, 0.1, 0.1 ) ); `