Search code examples
pythonseleniumxpathhrefnosuchelementexception

HTML Selenium Python click on href link


I would like to write a python code where he clicks on the href link.

Screenshot of HTML: screenshot of HTML code

This is what I am currently having in Python, but it does not work.

tables = browser.find_elements_by_xpath('//*[@id="notice"]')

for table in tables:
     row = table.find_element_by_xpath('//tr[@class="Zebra"]//td//a[@href="https://enot.publicprocurement.be/enot-war/preViewNotice.do?noticeId=438868&saveSearchParams=true&pageSize=%31%32%35&d-446978-p=%31&"]').click()

This is the error message for row:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//tr[@class="Zebra"]//td//a[@href="https://enot.publicprocurement.be/enot-war/preViewNotice.do?noticeId=438868&saveSearchParams=true&pageSize=%31%32%35&d-446978-p=%31&"]"}

Could you please tell me what I am doing wrong?


Solution

  • The desired element is a <a> element with the innerText as Universiteit Antwerpen-22004

    To click on the desired element you can use either of the following locator strategies:

    • Using the href attribute:

      table.find_element_by_xpath('.//tr[@class="Zebra"]//td//a[starts-with(@href, "https://enot.publicprocurement.be/enot-war/preViewNotice.do?noticeId")]').click()
      
    • Using the innerText:

      table.find_element_by_xpath('.//tr[@class="Zebra"]//td//a[contains(., "Universiteit Antwerpen-22004")]').click()