0

I want to click on an element which contains class and title in selenium python.

A webpage contains certain folders with unique name. I want to click on a folder by specifying its name or title. Below is the html tag. I tried many ways but I am ending up with errors.

<div class="folder">
<p>
<a alt="ABC" title="ABC">ABC</a>
</p>
</div>

Initially, it used to work with below :

driver.find_element_by_xpath("//a[@title='ABC']").click()

But now it says unable to locate element. Please help me to solve this.

Error:

"selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element"
4
  • 2
    What happens when you run this code? Does the find_element_by_xpath raise an exception or do you get a wrong element? May 13, 2021 at 13:58
  • And, you keep mixing up "alt" and "title". So, which attribute are you wanting to use?
    – Lee Jensen
    May 13, 2021 at 14:25
  • I want to use title. Please help me with this.
    – Arpitha M
    May 17, 2021 at 5:51
  • I get "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element" error.
    – Arpitha M
    May 17, 2021 at 5:56

1 Answer 1

2

It looks like the OPs xpath selection is correct in order to find an element by title. To me, an error of "NoSuchElementException" occurs because the element is not in the DOM, not displayed, not clickable, etc before Selenium tries to interact with it.

The common solution here is to wait for the page to load or wait for element to be displayed. While I have not coded in python, a quick search led me to the following.

An example of this can be found here.

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.Xpath, "//a[@title='ABC']")))

This link also lists other wait conditions.

  • title_is
  • title_contains
  • presence_of_element_located
  • visibility_of_element_located
  • visibility_of
  • presence_of_all_elements_located
  • text_to_be_present_in_element
  • text_to_be_present_in_element_value
  • frame_to_be_available_and_switch_to_it
  • invisibility_of_element_located
  • element_to_be_clickable
  • staleness_of
  • element_to_be_selected
  • element_located_to_be_selected
  • element_selection_state_to_be
  • element_located_selection_state_to_be
  • alert_is_present

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.