0

I am trying to locate an href containing the substring '.ics', such as in the screenshot, and return the link as a string. screenshot

Below is my code attempt:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get('http://miamioh.edu/emss/offices/career-services/events/index.html')

element = driver.find_element_by_partial_link_text('.ics')

However, I get this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":".ics"}

No doubt I am overlooking something very basic, but I can't figure out what. I have also tried the line

element = driver.findElement(By.cssSelector("a[href*='services.ics']")).click();

instead of the other line beginning with 'element'. However, this gives

AttributeError: 'WebDriver' object has no attribute 'findElement'
1
  • 2
    That's not the link text. Commented Jul 12, 2017 at 16:35

4 Answers 4

4

The link text is exact text of the link you see on web page while partial link text is just some substring of that link text.

"services.ics" is part of href attribute. If you want to find element by "services.ics" you might use

driver.find_element_by_xpath('//a[contains(@href, "services.ics")]')

Also you might use title attribute to match required element:

driver.find_element_by_xpath('//a[@title="iCal Feed"]')

Note that

element = driver.findElement(By.cssSelector("a[href*='services.ics']")).click();

is Java analogue of Python code

from selenium.webdriver.common.by import By 
element = driver.find_element(By.CSS_SELECTOR, "a[href*='services.ics']").click();

Update

Link might be generated dynamically, so you can try to apply ExplicitWait as below to avoid NoSuchElementException:

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

wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[@title="iCal Feed"]'))).click()

Update 2

As target link located inside an iframe you should switch to that frame before clicking link:

wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("trumba.spud.1.iframe"))
driver.find_element_by_xpath('//a[@title="iCal Feed"]').click()
Sign up to request clarification or add additional context in comments.

13 Comments

Hi @Andersson, thanks for your very detailed resonse. However, when I try running this code driver.find_element_by_xpath('//a[contains(@href, "services.ics")]') I receive NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"a[href$=".ics"]"} (Session info: chrome=59.0.3071.115)
Received a TimeoutException: Message: by applying the ExplicitWait. Would this mean the link is not generated dynamically?
This might mean that link located inside an iframe. Can you check if there is an <iframe> element among link ancestors?
It could also mean there are more than one element that has the "services.ics" href or the "title" is used more than once via the a tag.
@MQ1217, trumba.spud.1.iframe is iframe ID. If you need general selector you can try wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_tag_name('iframe'))), but this will allow you to switch to first iframe on the page. If target element located inside of another iframe, you should use more specific selector
|
1

In Python the Selenium method is driver.find_element. Also partial link text does not refer to the link, instead it refers to the text in the "a" tag. i.e.

<a href="link.com">this is the link text</a>

3 Comments

Hi @TitusLucretius thanks for your response. I get this error? File "python_org_search.py", line 11, in <module> driver.find_element(By.cssSelector("a[href*='services.ics']")).click() NameError: name 'By' is not defined
This is not really an answer, but a critique. This should of been a comment.
My bad, at this point the other answers seem to have the solution I would point to anyway.
1

Since you want to search for a <a href="..."> that ends with .ics. We can do this with a CSS selector, like:

a[href$=".ics"]

So we can use the following code:

element = driver.find_element_by_css_selector('a[href$=".ics"]')

Or if you are looking for 'services.ics':

element = driver.find_element_by_css_selector('a[href$="services.ics"]')

1 Comment

Hi, thanks for your response. Unfortunately I am getting NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"a[href$=".ics"]"} when I try running the above code. Any ideas?
1

You need to get the attribute href. First locate that element without looking for the "HREF", then get the attribute.

myHrefVal = element.get_attribute('href')
print(myHrefVal)

As @Andersson has stated, try using the xPath.

driver.find_element_by_xpath('//a[@title="iCal Feed"]')

Good luck! :)

4 Comments

Hi @IamBatman thanks for your answer. However, I tried using some of the other responses to locate the element in the first place but I am still receiving errors as mentioned in the comments... do you have any input on how to code to locate element? Thanks
Have you tried the "title" xpath as @Andersson has stated?
Yeah, just as I expected (went straight to your site page). The "services.ics" and the "iCal Feed" is found twice. BUT it's only found once if you reference the "a" tag as @Andersson has stated. Please try WITHOUT the explicit wait an just find the element as you have been trying with just "//a[@title='iCal Feed']". I updated my answer.
@MQ1217 it seems you had an iframe wrapped around the opposing element. Andersson has helped you with the locating of the element, check his updated answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.