user2770624 user2770624 - 1 year ago 65
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.


{% 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 %}

{% 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 Source

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

from import By
from import WebDriverWait
from 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)