Selenium: finding element by href

How would you find an element in selenium (with python) of the following html:

<a href="/user/login" onclick="ttweb.UserOperations.Login.OpenLoginPopup({ LoginUrl: '/user/login', RedirectUrl: '/' }); return false;">Login</a>

Code

1 answer

  • answered 2017-11-15 05:15 DebanjanB

    As per the HTML you have shared to find the element by href you can use either of the following:

    1. css_selector:

      driver.find_element_by_css_selector("a[href=/user/login]")
      
    2. xpath:

      driver.find_element_by_xpath("//a[@href='/user/login']")
      
    3. xpath (Multiple Attributes):

      driver.find_element_by_xpath("//a[@href='/user/login' and text()='Login']")