1

HTML:

<a title="ITC" href="javascript:void(0);" onclick="TPComView(&quot;2020-2021&quot;,&quot;39&quot;);">ITC</a>

I tried using cssSelector and xpath but it shows error like:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
DebanjanB
  • 118,661
  • 30
  • 168
  • 217

2 Answers2

0

You can use the find_element_by_xpath function.

driver.find_element_by_xpath('//a[@href="'+url+'"]')
eirikg
  • 19
  • 3
0

To locate the element you can use the title, onclick or/and innerText attributes and you can use either of the following Locator Strategies:

  • Using link_text:

    element = driver.find_element_by_link_text("ITC")
    
  • Using css_selector:

    element = driver.find_element_by_css_selector("a[title='ITC'][onclick^='TPComView']")
    
  • Using xpath:

    element = driver.find_element_by_xpath("//a[@title='ITC' and text()='ITC']")
    

Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using LINK_TEXT:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.LINK_TEXT, "ITC"))).click()
    
  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[title='ITC'][onclick^='TPComView']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@title='ITC' and text()='ITC']")))
    
  • 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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217