我在测试时遇到了selenium无法在HTML中查找元素的问题。我试过按ID,名称和标签名称查找元素。这些方法都不起作用。
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 %}
测试,这里是一些无效的测试代码。我检查了它是否检查了错误的页面,并确认测试正在使用正确的HTML:
foos = self.browser.find_element_by_id('id_foo')
self.assertIn('dayofservice', foos.text)
答案 0 :(得分:1)
在黑暗中拍摄,但您可能需要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)