Python - Selenium Locate elements by href

11,812

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

Share:
11,812
aaaaaaaaaron_g
Author by

aaaaaaaaaron_g

I'm working on a supreme bot in python, but I have little experience in code.

Updated on June 04, 2022

Comments

  • aaaaaaaaaron_g
    aaaaaaaaaron_g almost 2 years

    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.