Selenium click link with no class or ID

How do I click this link using Selenium in Python? I’ve been able to get to the webpage using Selenium and click other links that have IDs. But this one doesn’t have an ID or name.

<div ng-repeat="dashboard in $ctrl.dashboards track by $index" class="btn btn-primary ng-scope active" ng-class="{active: dashboard.Dashboard_Id == $ctrl.activeDashboard.Dashboard_Id}" ng-click="$ctrl.dashboardClick(dashboard)" style="">
     <span ng-bind="dashboard.Name" class="ng-binding">Hours By Activity</span>
</div>

I’ve tried this with no luck:

driver.find_elements_by_xpath("//*[contains(text(), 'Hours by Activity')]")

>Solution :

The desired element is an Angular element so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.ng-binding[ng-bind='dashboard.Name']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='ng-binding' and @ng-bind='dashboard.Name'][text()='Hours By Activity']"))).click()
    
  • Note: You have to add the following imports :

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

Leave a Reply