Home > front end >  Selenium: How to click on a link with no class or ID
Selenium: How to click on a link with no class or ID

Time:06-18

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.

HTML:

<div ng-repeat="dashboard in $ctrl.dashboards track by $index"  ng- ng-click="$ctrl.dashboardClick(dashboard)" style="">
     <span ng-bind="dashboard.Name" >Hours By Activity</span>
</div>

I've tried this with no luck:

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

CodePudding user response:

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
    
  • Related