Josh
Josh

Reputation: 107

Grabbing an html element using Selenium and partial_link_text

I am trying to grab a link element using Selenium's driver.find_element_by_partial_link_text in the Firefox Driver.

The reason I am using this is because the full name of the link will be changing periodically, but the last few characters will always stay the same. Something like: "[...] AL Changes.xlsx"

For some reason, I am getting an "NoSuchElementException: Unable to locate element" error

Here is the html...

<a class="ms-listlink ms-draggable" onmousedown="return VerifyHref(this,event,'1','SharePoint.OpenDocuments','')" onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','SharePoint.OpenDocuments.3','1','SharePoint.OpenDocuments','','','','2027','0','0','0xb008431061')" href="/sites/market/Market_And_Customer_Information/MCIR Library/01 2015 AL Changes.xlsx" DragId="3">01 2015 AL Changes</a>​

and here is my code:

self.driver.get(url_path)
self.driverfind_element_by_partial_link_text('AL Changes.xlsx').click()

enter image description here

Upvotes: 1

Views: 276

Answers (1)

alecxe
alecxe

Reputation: 473753

You are confusing the link text with an href attribute value.

The link text in your case is "01 2015 AL Changes":

self.driver.find_element_by_link_text('01 2015 AL Changes').click()

If you, though, need to use the href attribute to locate the element:

self.driver.find_element_by_css_selector('a[href$="AL Changes.xlsx"]').click()

where $= means "ends with".


You may also need to wait for the link to become clickable:

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "01 2015 AL Changes")))
link.click()

Upvotes: 2

Related Questions