Python - Selenium Locate elements by href
Question: I am trying to find (and click) specific pages for my Python program using
selenium.

I have tried

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

And

    
    
    driver.findElement(By.partialLinkText("text")).click();
    

With `text` what I am searching for. These do not work with the error

    
    
    AttributeError: 'WebDriver' object has no attribute 'findElement'
    

I can only assume that it can't do find element because that's for Java, not
Python.

The only distinguishing factor to the links on the website is the href
attributes.

All the other tags have repeats. There is no way I can 100% guess the right
link, so I additionally need the locator to be by partial text.

Is there anyway to start this? Could I use other programs? Has anybody
successfully done this? I tried searching but nobody has even asked this.


Answer: You can try:

    
    
    from selenium.webdriver.common.by import By
    ...
    driver.find_element(By.PARTIAL_LINK_TEXT, 'text').click()
    

or

    
    
    from selenium import webdriver
    ...
    driver.find_element_by_partial_link_text("text").click()
    

**EDIT:** For partial text search on the href itself try:

    
    
    from selenium import webdriver
    ...
    driver.find_element_by_xpath("//a[contains(@href,'text')]").click()
    

Sources:

<http://selenium-python.readthedocs.org/faq.html?highlight=click#how-to-auto-
save-files-using-custom-firefox-profile>

<http://selenium-python.readthedocs.org/locating-elements.html#locating-
elements>

