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")
2
  • 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 = """ <html> <head></head> <body> <a href="#" onclick="javascript:toMove('0000172','c99','boo.com/usemn/co/…);" onfocus="blur()"> foo </a> </body> </html> """ 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) Commented Jun 16, 2023 at 2:55
  • Check this answer if it helps - stackoverflow.com/a/75865161/7598774 Commented Jun 16, 2023 at 11:07

1 Answer 1

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
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.