fabio
fabio

Reputation: 1365

find_element_by_link_text should find this link?

Quick question.

I have this code:

    albums_links = self.browser.\
        find_elements_by_link_text('Albums')
    # albums_links = self.browser.find_elements_by_xpath(
    #    '//a[contains(@href, "albums")]') # this works (enough), I leave here for reference
    self.assertEqual(albums_links[0].get_attribute('href'),
        self.live_server_url + '/admin/albums/')
    self.assertEqual(albums_links[1].get_attribute('href'),
        self.live_server_url + '/admin/albums/album/')

It was working. Now I have reinstalled OS, redone the venv etc and the code isn't working anymore. In theory the only things different is the python version and the django version but I'm not sure because the changes are big.

So, I ask if that code should find both those links or only one:

<caption>
    <a href="/admin/albums/" class="section" title="Models in the Albums application">Albums</a>
 </caption>
     <tr class="model-album">
         <th scope="row"><a href="/admin/albums/album/">Albums</a></th>

Before (python 3.4 django 1.8) it found both, now (python 3.7.4, django 2.0) only the second one. This behavior is consistent and work in the same way for other links in some "frame" like Albums or Solos and then in Add Track (is a standard django admin)

enter image description here

I'd see that In selenium the comand find_element_by_link_text isn't reliable but is this a feature?

Thank you

Edit: in other words maybe the class and the title inside the elements can be a problem?

Html for "Add track":

<li>
   <a href="/admin/albums/track/add/" class="addlink">
     Add track
   </a>
</li>

Edit2: also the django version is different. Maybe something changed in the html?

Upvotes: 0

Views: 67

Answers (1)

CEH
CEH

Reputation: 5909

Rather than using find_element_by_link_text, you could try XPath instead.

driver.findElement(By.XPath("//a[contains(text(), 'Add track')]"));

This is less complicated than find_element_by_link_text, and it will not break if the link changes, since we are only looking at the plain text inside the tag.

Hope this helps.

Upvotes: 1

Related Questions