Nanor
Nanor

Reputation: 2550

Selenium find_element_with_link_text not working

I have a piece of code as follows

<a class="country" href="/es-hn">
    Honduras
</a>

and I'm trying to assign to to a variable by doing

el = self.driver.find_element_by_link_text('Honduras')

However whenever I run it I get the following error:

NoSuchElementException: Message: u"Unable to find element with link text == Honduras"

Upvotes: 2

Views: 260

Answers (3)

Amith
Amith

Reputation: 7008

I agree with sircapslot, in this case partial link text would also work:

el = self.driver.find_element_by_partial_link_text('Honduras')

Upvotes: 1

ddavison
ddavison

Reputation: 29062

I've seen link_text fuzz up when trying to find a link when it's in block formation like this. I think it has something to do with the tabulation:

<a class="country" href="/es-hn">
[    ]Honduras
</a>

It only seems to work consistently when in line like this:

<a class="country" href="/es-hn">Honduras</a>

Try this:

el = self.driver.find_element_by_css_selector("a.country[href$='es-hn']")

Upvotes: 2

Drewness
Drewness

Reputation: 5072

This may occur when selenium is trying to find the link while your application hasn't rendered it yet. So you need to make it wait until the link appears:

browser.implicitly_wait(10) # 10 seconds
el = self.driver.find_element_by_link_text('Honduras')

The implicitly_wait call makes the browser poll until the item is on the page and visible to be interacted with.

Upvotes: 0

Related Questions