jakethedog
jakethedog

Reputation: 350

selenium python find link by href text and click it

I have my script to login to a site. i then need to click on another link which is contained in an

<a href> </a>

I have tried multiple methods without success. The link I need "Available Deployments" only appears after clicking a dropdown box called "Job Board".

The site code looks like this:

<li class="">
    <a aria-expanded="false" class="dropdown-toggle" data-toggle="dropdown" href="portalPost?s=a1W390000045MxAEAU&amp;p=a1V39000003y7e1EAA" role="button">Job Board <span class="caret"></span>
    </a>
<ul class="dropdown-menu" role="menu">

<li>
    <a href="portalPage?s=a1W390000045MxAEAU&amp;p=a1V39000003y7dbEAA">Available Deployments
    </a>
</li>

i've tried a couple of versions, without success:

-SNIP-
driver.find_element_by_name("logmein").click()
driver.find_element_by_linkText("Job Board").click()    
driver.find_element_by_linkText("Available Deployments").click()

and

-SNIP-
driver.find_element_by_name("logmein").click()
driver.find_element_by_xpath(u'//a[text()="Job Board"]').click()
driver.find_element_by_xpath(u'//a[text()="Available Deployments"]').click()

The errors I get typically look like:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[text()="Available Deployments"]"}

Upvotes: 3

Views: 11451

Answers (2)

jakethedog
jakethedog

Reputation: 350

I solved this with the following:

driver.find_element_by_link_text("Job Board").click()
driver.find_element_by_link_text("Available Deployments").click()

Upvotes: 5

Buaban
Buaban

Reputation: 5137

It looks like there is a whitespace in text of the element. You have to use normalize-space to trim text. See an example below.

driver.find_element_by_xpath(u'//a[text()="Job Board"]').click()
waitForPresence = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.XPATH,'//a[normalize-space(text())="Available Deployments"]')))
driver.find_element_by_xpath('//a[normalize-space(text())="Available Deployments"]').click()

Upvotes: 7

Related Questions