0

I am trying to click on an element using Selenium but keep getting the error message "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element {"method":"partial link text","selector":"foo"}" . Here is the element I want to click:

<a href="#" onclick="javascript:toMove('0000172','c99','https://www.boo.com/usemn/co/UM_COJ_CIQ003.jsp','mainFrame','PT_FMJ_SFQ001.jsp');" onfocus="blur()">
    foo
</a>

And here is the code I'm using in Selenium:

link = driver.find_element(By.PARTIAL_LINK_TEXT, "foo")
Diego Quirós
  • 898
  • 1
  • 14
  • 28
  • I tried literally the same code and was able to get the element. For reference here is the code to test, I didn't include the imports html_content = """ foo """ html_bs64 = base64.b64encode(html_content.encode('utf-8')).decode() driver.get("data:text/html;base64," + html_bs64) link = driver.find_element(By.PARTIAL_LINK_TEXT, "foo") print(link.text) – ROOP AMBER Jun 16 '23 at 02:55
  • Check this answer if it helps - https://stackoverflow.com/a/75865161/7598774 – Shawn Jun 16 '23 at 11:07

1 Answers1

0

Given the HTML:

<a href="#" onclick="javascript:toMove('0000172','c99','https://www.boo.com/usemn/co/UM_COJ_CIQ003.jsp','mainFrame','PT_FMJ_SFQ001.jsp');" onfocus="blur()">
    foo
</a>

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

  • Using partial_link_text:

    driver.find_element(By.PARTIAL_LINK_TEXT, "foo").click()
    
  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "a[onclick*='mainFrame']").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//a[contains(@onclick, 'mainFrame') and contains(., 'foo')]").click()
    

However, the desired element is a dynamic 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 PARTIAL_LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "foo"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick*='mainFrame']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@onclick, 'mainFrame') and contains(., 'foo')]"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352