user2770624 user2770624 - 5 months ago 20
HTML Question

Selenium not finding elements in HTML

I have an issue with selenium not finding elements in my HTML when I a testing. I have tried finding elements by id, name, and tag name. None of these methods work.

HTML:

{% extends 'base.html' %}
{% block body %}

<h2 id='id_foo'>{% for foo in foo_list%}
#{{ foo.slug }}
{% endfor%}</h2>
<form method="POST" action='/'>
<input name='add_foos' placeholder='MORE FOOS!' id='id_add_foo'/>
{% csrf_token %}
</form>


{% endblock %}


test, here is the bit of testing code that is not working. I have checked to see if it was checking the wrong page, and have confirmed that the test was working on the right HTML:

foos = self.browser.find_element_by_id('id_foo')

self.assertIn('dayofservice', foos.text)

Answer

Shot in the dark, but you might just need to give time the element to become present:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(self.browser, 10)
foos = wait.until(EC.presence_of_element_located((By.ID, 'id_foo')))

self.assertIn('dayofservice', foos.text)