当前位置: 首页 > 知识库问答 >
问题:

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

郁明诚
2023-03-14
  <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:消息:找不到元素:{“Method”:“XPath”,“Selector”:“//A[@href=\”WLSwitchInterFace0.wl\“]”}

共有1个答案

芮瑾瑜
2023-03-14

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

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

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()
 类似资料:
  • 我突然觉得点击链接有问题。这是以前的工作,但现在不是,我不能找出什么是错的。 这是链接的定义方式: 这是我点击链接的代码。其中有几个,我想做的是获取所有这些,这样我就可以遍历所有这些。 它没有通过List语句。它就坐在那里。有什么想法吗?

  • 我正在使用selenium网络驱动程序来自动化网页。我的selenium代码没有识别链接。我收到以下错误。 线程“main”组织中出现异常。openqa。硒。NoSuchElementException:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“/html/body/font/font/b/a[2]”(会话信息:chrome=44.0.2403.89

  • 当我运行此代码时,会出现以下错误:- 组织。openqa。硒。StaleElementReferenceException:stale元素引用:元素未附加到页面文档 我也尝试了含蓄的等待,但得到了同样的问题。

  • 我正在做一个网络抓取,我可以在一个特定的链接中获取信息,但是我想通过去那个页面上每个项目的链接来自动化这个过程,然后获取信息回到主页,然后点击下面项目的链接。 网址是:https://ca.iherb.com/c/Vitamins?noi=48 我想点击“加州黄金营养、黄金C、维生素C、1000毫克、60粒蔬菜胶囊”项目,然后点击底部的“查看所有评论”。然后返回主页并单击以下项目的链接。 我使用此

  • 问题内容: Selenium WebDriver出现问题。我尝试单击窗口页面外部的链接(您需要向上滚动才能看到它)。我当前的代码是相当标准的: 我知道我可以向上滚动,并且在这种情况下可以使用。但是,如果您有很长的项目列表,则不必知道向下滚动的距离。 有什么方法可以单击页面可见部分之外的链接(但是如果滚动,该链接将可见)? 附带说明,我使用的是Firefox,但我计划也使用IE7 / 8/9和Chr

  • 问题内容: 我需要自动单击网页中的任何“添加”按钮,如下所示: “ https://groceries.asda.com/search/yoghurt ” 但是,页面中的“添加”按钮都没有名称或ID。所以我不能使用Selenium包中的命令。 谁能帮我? 问题答案: 要单击特定产品的任何特定按钮,可以编写如下方法: 现在,您可以单击传递其标题的任何按钮,如下所示: