Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

python - Selenium webdriver is not finding the Inbox element

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.

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.

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']")

All the locators I have tried raise 'selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element'

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>

Thanks for the help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To click the element with text as Inbox you can use either of the following solutions:

  • css_selector:

    find_element_by_css_selector("td.MENUCHOICE a[href='main.do?action=inbox']")
    
  • xpath:

    find_element_by_xpath("//td[@class='MENUCHOICE']//a[@href='main.do?action=inbox' and contains(.,'Inbox')]")
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...