我想这是那些永恒的问题之一,但是我需要一些有关XPath表达式的帮助。的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>
Python中的搜索表达式如下:
self.wait_for(lambda: self.assertEqual(
self.browser.find_element_by_xpath(
"//span[contains(text(), 'You can't have an empty list item')]"
)
)
)
这是在测试中运行的,即使明显存在,也无法找到文本。从测试的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 test_cannot_add_empty_list_items
self.wait_for(lambda: self.assertEqual(
File "/home/eric/Git/TDD/functional_tests/base.py", line 40, in wait_for
raise e
File "/home/eric/Git/TDD/functional_tests/base.py", line 37, in wait_for
return fn()
File "/home/eric/Git/TDD/functional_tests/test_list_item_validation.py", line 17, in <lambda>
"//span[contains(text(), 'You can't have an empty list item')]"
File "/home/eric/Git/TDD/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/home/eric/Git/TDD/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/home/eric/Git/TDD/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/eric/Git/TDD/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //span[contains(text(), 'You can't have an empty list item')]
----------------------------------------------------------------------
Ran 4 tests in 34.851s
FAILED (errors=1)
编辑:断言应该是assertTrue而不是assertEqual,因为我没有将结果与任何东西进行比较。
答案 0 :(得分:7)
HTML文档中没有'
。有一个'
。
'
仅通知HTML解析器在该位置的文档树中插入单引号,实际上并没有最终成为您可以搜索的内容。
您可以这样做:
self.wait_for(lambda: self.assertEqual(
self.browser.find_element_by_xpath(
'//span[contains(text(), "You can\'t have an empty list item")]'
)
)
)
但这仅在引号正好以此方式的情况下才有效。当您的搜索文本中包含双引号时,以上内容将中断,并且您必须以相反的方式进行转义。只要预定义了搜索文本,这是可行的。
只要生成的XPath有效,就可以了。在这种情况下,上面的代码将生成这个完全有效的XPath表达式:
//span[contains(text(), "You can't have an empty list item")]
但是,如果搜索文本是可变的(例如,用户定义的),则内容会变得冗长。 Python知道字符串转义序列,您始终可以使用\"
或\'
来获取字符串的引号。 XPath不知道这件事。
假定搜索文本为You can't have an "empty" list item
。这很容易用Python生成,但是不起作用:
//span[contains(text(), "You can't have an "empty" list item")]
-------------------------------------------^ breaks here
此XPath也不起作用:
//span[contains(text(), 'You can't have an "empty" list item')]
--------------------------------^ breaks here
,这个也不会,因为XPath没有转义序列:
//span[contains(text(), 'You can\'t have an "empty" list item')]
---------------------------------^ breaks here
在XPath中可以解决此问题的方法是将不同引号的字符串连接在一起。这个:
//span[contains(text(), concat('You can', "'" ,'t have an "empty" list item'))]
完全有效,将搜索文本You can't have an "empty" list item
。
您可以在Python中执行以下操作:
'
', "'", '
concat('
前面加上')
以下内容将允许由于XPath格式错误而永远不会引发运行时错误的字符串搜索:
search_text = 'You can\'t have an "empty" list item'
concat_expr = "', \"'\", '".join(search_text.split("'"))
concat_expr = "concat('" + concat_expr + "')"
xpath = "//span[contains(text(), %s)]" % concat_expr
xpath
,作为Python字符串文字(将其打印到控制台时会看到的内容):
'//span[contains(text(), concat(\'You can\', "\'", \'t have an "empty" list item\'))]'
XPath引擎查看它的方式(即内存中的实际字符串):
//span[contains(text(), concat('You can', "'", 't have an "empty" list item'))]
lxml库允许使用XPath variables,比这还优雅得多,但是我怀疑Selenium的find_elements_by_xpath
是否支持它们。
答案 1 :(得分:0)
@Tomalak答案使我们对{em> xpath 的text()
有深刻的了解。但是,当您使用find_element_by_xpath()
时,可以放心使用 class 属性,并且可以使用以下基于 xpath 的解决方案:
self.wait_for(lambda: self.assertEqual(
self.browser.find_element_by_xpath(
"//span[@class='help-block' and contains(., 'have an empty list item')]"
)
)
)