Search code examples
djangoseleniumdjango-testing

NoSuchElementException, django, selenium web driver


I'm trying to write a series of tests for mine application, so I'm starting with basic, but there is small error and I'm facing NoSuchElementException because I'm not correctly fetching hyperlinks by link text.

I want to find and click on a hyperlink which will get me to about page link so I'm in functional_tests

def test_click_on_about_link_navbar_get_users_to_about_page(self):
    """TODO: Docstring for test_societe_navbar_get_user_to_about_page.
    :returns: Click on navbar and return about page

    """
    self.browser.find_element_by_link_text('About').click()
    self.assertIn('http://localhost:8000/about', self.driver.current_url)

and the element's I'm trying to find

<li><a href="{% url 'index' %}">Home</a></li>
<li><a href="{% url 'about' %}">About</a></li>

What I'm I missing, can someone explain me how to do this correctly.


Solution

  • Try this:

    from selenium.webdriver.support.ui import WebDriverWait
    WebDriverWait(self.browser, 10).until(lambda browser: self.browser.find_element_by_xpath('//li/a[contains(text(), "About")]'))