我正在尝试从字符串构建ElementTree.当我执行以下操作时(如Python ElementTree中所述:解析字符串并获取ElementTree实例),我得到一个Element而不是ElementTree:
companyTree = ElementTree.ElementTree(ElementTree.fromstring('<companies></companies>'))
Run Code Online (Sandbox Code Playgroud)
如果我做
print(companyTree.getroot())
Run Code Online (Sandbox Code Playgroud)
我明白了
AttributeError:'xml.etree.ElementTree.Element'对象没有属性'getroot'
换句话说,companyTree是Element而不是ElementTree.根据1中接受的答案,我应该得到一个ElementTree.API有变化吗?我如何从字符串中获取ElementTree?谢谢.
请注意,Python ElementTree中有更多关于此问题:ElementTree与root元素
我想这是一个永恒的问题,但我需要一些XPath表达式的帮助.使用Selenium搜索的HTML如下所示:
<div class="container">
<div class"row">
<div class="col-md-6 col-md-offset-3 jumbotron">
<div class="text-center">
<h1>Start a new To-Do list</h1>
<form method="POST" action="/lists/new">
<input name="item_text" id="id_new_item"
class="form-control input-lg"
placeholder="Enter a to-do item" />
<input type="hidden" name="csrfmiddlewaretoken" value="***********">
<div class="form-group has-error">
<span class="help-block">You can't have an empty list item</span>
</div>
</form>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Python中的搜索表达式如下所示:
self.wait_for(lambda: self.assertEqual(
self.browser.find_element_by_xpath(
"//span[contains(text(), 'You can't have an empty list item')]"
)
)
)
Run Code Online (Sandbox Code Playgroud)
这是在测试中运行的,即使显然存在,也无法找到文本.测试的ttaceback是:
ERROR: test_cannot_add_empty_list_items (functional_tests.test_list_item_validation.ItemValidationTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/eric/Git/TDD/functional_tests/test_list_item_validation.py", line 15, in …Run Code Online (Sandbox Code Playgroud)