Reputation: 37
I am trying to locate an href containing the substring '.ics', such as in the screenshot, and return the link as a string.
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'
Upvotes: 0
Views: 1529
Reputation: 52665
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()
Upvotes: 4
Reputation: 1015
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! :)
Upvotes: 1
Reputation: 476659
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"]')
Upvotes: 1
Reputation: 181
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>
Upvotes: 1