简体   繁体   中英

Selenium Webdriver (Java): Why is it that my text link can't be found?

This test was working yesterday but now all of the sudden it doesn't seem to work. I'm trying to click on a text link. Any help would be appreciated.

My code:

driver.findElement(By.linkText("ALEX")).click();
Thread.sleep(5000); 

HTML:

<body>




    <div id="page">

<!-- include Header Start -->
            <a name="PAGETOP"></a>




        <div id="header">


            <div class="smallmenu" >

                            <a href= "BatchProcess.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">Batch Processing</a> |

                            <a href="JobQueue.asp?PPVS=CELL5572&PF=SECURITY" title="Click here to view the job queue">Job Queue</a> |

                            <a href= "PDMain.asp?PPVS=CELL5572" title="Setup">CELL5572</a> |

    <a href="mailto:ITS@test.com?subject=CELL5572&body=%0D%0A%0D%0A______________________________________________________%0D%0A%0D%0A%0D%0ASite: CELL5572%0D%0APage: /btrac/Security.asp%0D%0Ahttps%3A%2F%2Ffrigg%2Fbtrac%2FSecurity%2Easp%3FPF%3DPDMain%26PPVS%3DCELL5572" Title="Send Internal Support Request">ITS</a>
    &nbsp;&nbsp;
                        <input type="text" class="dataEntryObject" name="TBPARAM" value="Site Search" onclick="if (this.value=='Site Search') this.value='';" onchange="if (this.value) {window.location='pdsearchresults.asp?PPVS=CELL5572&PAGE=SITE&TBPARAM=' + this.value;}">
                        &nbsp;&nbsp;
                        <a href="Site.asp?PPVS=CELL5572"><img class="systemAreaIcon" src ="public/images/icons/icon-home.png" alt="Home" title="Home"/></a>

                <a href="javascript:spawn_window('help.asp?PPVS=CELL5572','600','800','',0)" ><img  class="systemAreaIcon" src="public/images/icons/icon-help.png" alt="Help" title="Help" /></a>

                    <a href="logout.asp?PPVS=CELL5572">Log Out</a>


            </div>


            <div class="client_logo">

                <img src="images/_SITE/CELL5572_logo.jpg" alt="Group"   />

            </div>
            <div class="mainmenu" >
                <div class="topBorderLine"></div>
                <table width="100%" cellpadding="0" cellspacing="0">
                <tr>

                    <!--[if IE]>
                        <td><div id="menuTitle" title="Benefits" >Benefits</div></td>
                    <![endif]-->
                    <!--[if !IE]><!-->
                        <td><div id="menuTitle" class="reflected" title="Benefits" >Benefits</div></td>
                    <!--<![endif]-->

                    <td align="justify">



                        <ul class = "menuUL">
                            <li class = "menuLI"><a href="#">Resources</a> 
                                <ul class = "menuUL">

                                        <li class = "menuLI"><a href="ReconStatus.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">List Bill Audit History</a></li>

                                    <li class = "menuLI"><a href="news.asp?PPVS=CELL5572&PF=SECURITY">News &amp; Alerts</a></li>
                                    <li class = "menuLI"><a href="javascript:spawn_window('whatsnew.asp?PF=Employer&PPVS=CELL5572','600','800','',0)">Release Notes</a></li>

                                        <li class = "menuLI"><a href="javascript:spawn_window('Library.asp?PPVS=CELL5572','600','1000','',0)">Resource Library</a></li>

                                </ul>            
                            </li>

                            <li class = "menuLI"><a href="#">Tools</a>
                                <ul class = "menuUL">
                                    <li class = "menuLI"><a href="BillingAudit.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">Billing Audit</a></li>

                                    <li class = "menuLI"><a href="EnrollTracking.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">Enroll Tracking</a></li>

                                    <li class = "menuLI"><a href="Preview.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">Preview Pending</a></li>

                                            <li class = "menuLI"><a href="Report.asp?PPVS=CELL5572">Report Factory</a></li>                                 

                                    <li class = "menuLI"><a href="SendSecureDownload.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">Secure Download Center</a></li>
                                    <li class = "menuLI"><a href="upload.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY," target="_blank">Secure Upload Center</a></li>

                            </ul>
                        </li>

                        <li class = "menuLI"><a href="#">Setup</a>
                            <ul class = "menuUL"  style="left: -106px!important;" >

                                <li class = "menuLI"><a href="MailRoom.asp?PPVS=CELL5572">Mail Room</a></li>                                    

                                <li class = "menuLI"><a href="colorsetup.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">Site Color</a></li>

                                <li class="menuLI"><a href="Contacts.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">Site Contacts</a></li>

                                <li class="menuLI"><a href="CreateNews.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">News &amp; Alerts</a></li>

                                <li class="menuLI"><a href="OpenEnroll.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">Open Enrollment</a></li>

                                <li class="menuLI"><a href="Resource.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">Resource Library</a></li>

                                <li class = "menuLI"><a href="Security.asp?PPVS=CELL5572&PF=SITE&HST=SECURITY,">Security</a></li>

                        </ul>
                    </li>
                </ul>



                    </td>
                </tr>
                </table>    

            </div>            
            <div class="relax">&nbsp;</div>

        </div>


<!-- include Header End  -->

        <div class="relax">&nbsp;</div>
        <br />


<!-- include Page Static Title Start -->



        <div id="pgTitle">

<!-- include BreadCrumbs Start -->               

    <div id="breadcrumb" >

    </div>

    <div class="divider">&nbsp;</div>

            <div class="search_header" >

        <form method="POST" action="Search.asp" name="SEARCHFRM">
            <input type="hidden" name="HST" value="SECURITY," />
            <input type="hidden" name="PF" value="SITE" />
            <input type="hidden" name="PPVS" value="CELL5572" />
            <table  cellpadding="0" cellspacing="0" style="border: 0; padding-left: 10px;">
                <tr>

                        <td>
                            <a href="demedit.asp?PPVS=CELL5572&PF=SITE&TABLE=EMPLOYEE&NS=NEW&SK=NEW&HST=SITE," >Add Employee</a>&nbsp;&nbsp;&nbsp;&nbsp;
                        </td>

                <td>        
                    <input type="hidden" name="STYPE2" value="E" />
                </td>
                <td>
                <input type="hidden" name="STYPE" value="ANY" />
                </td>
                <td>

                    <input type="text" id="QRY_header" name="QRY" size="16" class="search_header_input" value="EE Search"  onclick="if (this.value == 'EE Search') this.value = '';"/>
                </td>
                <td class="tdata" style="white-space: nowrap; border: none;">
                    <input type="submit" value=">>" class="commandButton" name="B1" />              

                        <label><input type="checkbox" id="allSitesSearch_header" name="ALLSITES" value="All" />Search All Sites</label>

                </td>

            </table>
        </form>     

            </div>

<!-- include BreadCrumbs End -->



            <div class="relax">&nbsp;</div>
            <div class="relax">&nbsp;</div>

            <div class="titleTextNoShade">

                <h2>Security</h2>
            </div>
            <div class="relax">&nbsp;</div>

        </div>
        <div class="relax">&nbsp;</div>
        <br />

<!-- include Page Static Title End -->

        <div class="relax">&nbsp;</div>
        <br />




        <div id="content" class="withTitle" >

<!-- include Notification Start -->

<!-- include Notification End -->

            <div class="relax">&nbsp;</div>

<!-- include Body-Cotent-Area Start -->
            <div class="relax">&nbsp;</div>

                        <form method="POST" action="SecurityEdit.asp" id="form1" name="form1">
                            <input type="hidden" name="PPVS" value="CELL5572">
                            <input type="hidden" name="PF" value="PDMAIN">
                              <input type="hidden" name="HST" value="SECURITY,">
                              <input type="hidden" name="SK" value="0">



                                  <table border="0" cellspacing="8"  width="99%">

                                               <tr bgcolor="#FFFFFF">
                                                  <td align="right">

                                                          <input type="submit" class="pbutton" value="Add New User" name="NEW">

                                                  </td>
                                              </tr>

                                  </table>



                                    <div class="tableholder" >  
                                    <table class="status textNormal noSort" width="100%" cellpadding="0" cellspacing="0" >

    <tr>
        <td>
            <h3>
            <span>Active Users</span>       
            </h3>
        </td>
    </tr>
    <tr>
        <th class="subhed">Name</th>
        <th class="subhed" nowrap>User Name</th>
        <th class="subhed">Email</th>
        <th class="subhed">Phone</th>
        <th class="subhed">Lvl</th>

            <th class="subhed">Role</th>
            <th class="subhed">Class</th>
            <th>Type</th>
    </tr>   

                                                <tr class="color1">

                                                    <td class="tdata" style="padding-left: 12px" valign="top">

                                                                <a href="SecurityEdit.asp?PPVS=CELL5572&SK=146887733&PF=PDMAIN&HST=SECURITY,">
                                                                    ALEX</a>

                                                    </td>
                                                    <td class="tdata" valign="top">ALEX</td>
                                                    <td class="tdata" valign="top">test0@test.com</td>
                                                    <td class="tdata" valign="top"></td>
                                                    <td class="tdata" valign="top">50</td>

                                                          <td class="tdata"></td>
                                                          <td class="tdata"></td>

                                                        <td  class="tdata" valign="top">Engineer&nbsp;</td>

                                                </tr>

                                                <tr class="color2">

                                                    <td class="tdata" style="padding-left: 12px" valign="top">

                                                                <a href="SecurityEdit.asp?PPVS=CELL5572&SK=242965156&PF=PDMAIN&HST=SECURITY,">
                                                                    ADMINQA8</a>

                                                    </td>
                                                    <td class="tdata" valign="top">ADMINQA8</td>
                                                    <td class="tdata" valign="top">test@test.com</td>
                                                    <td class="tdata" valign="top"></td>
                                                    <td class="tdata" valign="top">8</td>

                                                          <td class="tdata">Client Service Supervisor</td>
                                                          <td class="tdata">Base Class</td>

                                                        <td  class="tdata" valign="top">HR/Internal User&nbsp;</td>

                                                </tr>

 </table>
                                    </div>
                                    <br />

                                    <table border="0" cellspacing="8"  width="99%">    
                                        <tr bgcolor="#FFFFFF">
                                              <td align="right"><input type="submit" class="pbutton" value="Add New User" name="NEW"></td>
                                          </tr>
                                      </table>

                    </form>

Let me know if I'm missing something.

Thanks

I personally don't like using LinkText() since it can break anytime with a little ui change or duplicate values addition on the page. I suggest you use xpath

By byXpath = By.xpath ("//*[@id='form1']//a[contains(text(),'ALEX')]")
WebElement element = (new WebDriverWait(driver, 10))
   .until(ExpectedConditions.elementToBeClickable(byXpath ));
element.click();

Note: Wait can definitely be a problem also. So try using some explicit wait before looking for the element.

Edit: Try action builder

By byXpath = By.xpath ("//*[@id='form1']//a[contains(text(),'ALEX')]")
WebElement element = driver.findElement(By.xpath(byXpath)); 
Actions action = new Actions(driver);
action.moveToElement(element).build().perform(); 
driver.findElement(element).click();
driver.findElement(By.xpath("//a[normalize-space(text())='ALEX']")).click();
Thread.sleep(5000);

By the linkText(), the text that we are getting contains extra spaces, so we need to trim all those extra space and we do it by normalize-space(). You can use this function in another way as well. I do it by xpath.

Please try the following code:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.linkText("ALEX")).click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM