Selenium webdriver 找不到收件箱元素

Selenium webdriver is not finding the Inbox element(Selenium webdriver 找不到收件箱元素)

本文介绍了Selenium webdriver 找不到收件箱元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我还没有找到解决方案,所以从两个月前开始遇到这个问题.我正在使用 Firefox 驱动程序查找并单击名为收件箱"的超链接,但经过详尽尝试后,Selenium 仍然找不到该元素.任何帮助将不胜感激,因为我真的无法找到 Selenium webdriver 找不到该元素的原因.

Bumping this from two months ago since I have not found a solution. I am using Firefox driver to find and then click on a hyperlink named "Inbox" but after exhaustive attempts, Selenium still can't find the element. Any help would be appreciated as I genuinely have not been able to find a reason why Selenium webdriver cannot find the element.

在下面的 HTML 代码中,我考虑过可能有一个框架但找不到;如果有人可以确认以下代码中实际上没有 iframe,将不胜感激!我的脚本中也已经有一个 time.sleep(40) 允许有足够的时间让 webdriver 找到元素.所以我的想法已经不多了,但我想也许收件箱"超链接元素之前的#text元素(    &nbsp")可能会通过隐藏收件箱"而导致问题"元素?任何有关如何进行的提示将不胜感激;下面列出了我所有的解决方法.

In the below HTML code, I have considered that there might be a frame but none can be found; if someone could confirm that there in fact is no iframe in the below code that would be appreciated! I also already have a time.sleep(40) in my script to allow for enough time for webdriver to find the element. So I am running out of ideas, but was thinking that maybe the #text element ("    &nbsp") right before the "inbox" hyperlink element might be causing the issue by hiding the "inbox" element? Any tips on how to proceed would be appreciated; all my attempts at resolution are listed below.

以下是我的代码,直到出现此元素问题(URL 已被删除,因为它是专有的):

Below is my code up until this element issue comes up (URL has been removed as it is proprietary):

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get("[URL REMOVED]")
time.sleep(2)
elem1 = driver.find_element_by_css_selector('#main-content > div > div.col-md-4.radix-layouts-sidebar.sidebar-right.panel-panel.sidebar > div > div.panel-pane.pane-entity-field.pane-it-service-field-related-services.block > div > div > ul > li:nth-child(1) > a')
elem1.click()
#Logging in
time.sleep(40)
#Now in the System, click on Inbox Button
elem2 = driver.find_element_by_??? # Trying to figure out this code to find inbox element
elem2.click()

我尝试了以下方法来尝试查找元素:

I have tried the below in trying to find the element :

find_element_by_xpath('//td[@class=’MENUCHOICE’]//a[@href="main.do?action=inbox"]')
find_element_by_xpath('/html/body/table/tbody/tr[6]/td/a')
find_element_by_link_text('Inbox')
find_element_by_partial_link_text('Inb')
find_element_by_css_selector('body > table > tbody > tr:nth-child(6) > td > a')
find_element_by_css_selector("td.MENUCHOICE a[href='main.do?action=inbox']")

我尝试过的所有定位器都会引发 'selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element'

我无法提供该页面的链接,因为它是专有的,但下面我提供了页面 HTML,以防有人可以提供帮助(感兴趣的元素位于倒数第二个 MENUCHOICE 类:收件箱):

I cannot provide the link to the page as it's proprietary, but below I have provided the page HTML in case someone can assist (The element of interest is under the second to last MENUCHOICE class: Inbox):

 <html>
   <!--702.15-->
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <meta http-equiv="Cache-Control" content="no-cache">
      <meta http-equiv="Expires" content="0">
      <meta http-equiv="Pragma" content="no-cache">
      <title>UDM - Notification System</title>
      <style type="text/css">
         td.MENU1 {
         FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;
         FONT-SIZE: 18pt;
         FONT-WEIGHT: medium;
         color: navy;
         }
         td.MENU2 {
         FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;
         FONT-SIZE: 15pt;
         FONT-WEIGHT: medium;
         color: navy;
         }
         td.MENUCHOICE {
         FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;
         FONT-SIZE: 14pt;
         FONT-WEIGHT: medium;
         color: blue;
         }
      </style>
   </head>
   <body bgcolor="#ffffff" background="payadm/images/background.gif">
      <table width="750" border="0">
      <tbody>
         <tr>
            <td><img height="55" width="129" src="payadm/images/udm_cw100.gif"></td>
         </tr>
         <tr>
            <td>&nbsp;<img src="payadm/images/footerline.gif"></td>
         </tr>
         <tr>
            <td class="MENU1">&nbsp;&nbsp;AN Main Menu</td>
         </tr>
         <tr>
            <td> &nbsp;</td>
         </tr>
         <tr>
            <td class="MENUCHOICE">&nbsp;&nbsp;&nbsp;Notification Functions:</td>
         </tr>
         <tr>
            <td class="MENUCHOICE">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
               <a href="main.do?action=inbox">Inbox</a>
            </td>
         </tr>
         <tr>
            <td class="MENUCHOICE">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
               <a href="main.do?action=notificationSel">Notification Selection</a>        
            </td>
         </tr>

感谢您的帮助.

推荐答案

这个问题已经在一个月前解决了.事实证明,对于这个特定的 Web 应用程序,Selenium webdriver 需要从一开始就具有直接链接;否则它不会识别"它所在的页面.此网络应用程序通常不直接链接;您通常会通过发布链接的中间页面.

This issue was resolved a month ago. It turns out that for this particular web application, Selenium webdriver needed to have a direct link from the get-go; otherwise it would not "recognize" the page it was on. This web app is usually not linked to directly; you usually go through an intermediary page that has the link posted.

我通过将 Selenium 直接定向到 Web 应用程序来解决此问题,而不是先通过中间页面并单击 Web 应用程序链接.即:

I resolved this by directing Selenium to the web application directly instead of going through the intermediary page first and clicking on the web app link. I.e.:

    from selenium import webdriver
    import time

    driver = webdriver.Firefox()
    driver.get("[DIRECT LINK INSTEAD OF INTERMEDIARY PAGE LINK]")

希望这对以后遇到类似问题的人有所帮助.

Hope this helps anyone with a similar issue in the future.

这篇关于Selenium webdriver 找不到收件箱元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Selenium webdriver 找不到收件箱元素

基础教程推荐