Alex Rivera | Logout

How to use find_element_by_link_text() properly to not raise NoSuchElementException?

Asked 2011-08-04T04:23:31.577
20

I have a HTML code like this:

<div class="links nopreview"><span><a class="csiAction"
href="/WebAccess/home.html#URL=centric://REFLECTION/INSTANCE/_CS_Data/null">Home</a></span>&nbsp;•&nbsp;<span><span><a class="csiAction"
href="/WebAccess/home.html#URL=centric://SITEADMIN/_CS_Site">Setup</a></span>&nbsp;•&nbsp;</span><span><a
title="Sign Out" class="csiAction csiActionLink">Sign Out</a></span></div>

I would like to click on the link that has the text Home. As this Home link appears after login, I have a code like this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://myServer/WebAccess/login.html") # Load App page
elem = browser.find_element_by_name("LoginID") # Find the Login box
elem.send_keys("Administrator")
elem = browser.find_element_by_name("Password") # Find the Password box
elem.send_keys("Administrator" + Keys.RETURN)
#try:
elem = browser.find_element_by_link_text("Home")
elem.click()

The part till login works great. However the last but one line is problematic

elem = browser.find_element_by_link_text("Home")

It raises this NoSuchElementException where the Home link is there as you can see from the HTML code.

raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"link text","selector":"Home"}' 

Any guidance as to what I am doing wrong, please?

Edit
Report

1 Answer

2

Maybe the element you are looking for doesn't exactly match that text string? I know it can be tricky if it looks like it does on-screen, but sometimes there are oddities embedded like this simple markup "Home" or "Home" which makes the first char italic:

"<i>H</i>ome" is visually identical to "<em>H</em>ome" but does not match text.

Edit: after writing the above answer, I studied the question closer and discovered the HTML sample does show "Home" in plain text, but was not visible due to long lines not wrapping. So I edited the OP to wrap the line for readability.

New observation: I noticed that the Logout element has a "title" attribute, but the Home link element lacks such--try giving it one and using that.

answered 2011-10-25T18:19:46.627

Your Answer