Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]"}

like image 275
jakethedog Avatar asked Jul 11 '26 06:07

jakethedog


1 Answers

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()
like image 139
Buaban Avatar answered Jul 14 '26 17:07

Buaban



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!