ElT*_*966 1 python selenium xpath xpath-1.0 selenium-webdriver
我想这是一个永恒的问题,但我需要一些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 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)
Run Code Online (Sandbox Code Playgroud)
编辑:断言应该是assertTrue而不是assertEqual,因为我没有将结果与任何东西进行比较.
'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")]'
)
)
)
Run Code Online (Sandbox Code Playgroud)
但这只有在引号正是这样的情况下才有效.当您的搜索文本包含双引号时,上述内容会中断,您必须以相反的方式转义.只要搜索文本是预定义的,这是可行的.
只要得到的XPath有效,你就可以了.在这种情况下,上面的结果是完全有效的XPath表达式:
//span[contains(text(), "You can't have an empty list item")]
Run Code Online (Sandbox Code Playgroud)
但是如果搜索文本是可变的(例如用户定义的)那么事情会变得多毛.Python知道字符串转义序列,您可以随时使用\"或\'将引号引入字符串.XPath不知道这样的事情.
假设搜索文本为You can't have an "empty" list item.这很容易用Python生成,但它不起作用:
//span[contains(text(), "You can't have an "empty" list item")]
-------------------------------------------^ breaks here
Run Code Online (Sandbox Code Playgroud)
而这个XPath也不会工作:
//span[contains(text(), 'You can't have an "empty" list item')]
--------------------------------^ breaks here
Run Code Online (Sandbox Code Playgroud)
这个也不会,因为XPath没有转义序列:
//span[contains(text(), 'You can\'t have an "empty" list item')]
---------------------------------^ breaks here
Run Code Online (Sandbox Code Playgroud)
您可以在XPath中解决此问题的方法是连接不同的引用字符串.这个:
//span[contains(text(), concat('You can', "'" ,'t have an "empty" list item'))]
Run Code Online (Sandbox Code Playgroud)
是完全有效的,将搜索文本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
Run Code Online (Sandbox Code Playgroud)
xpath,作为Python字符串文字(当您将其打印到控制台时会看到的内容):
'//span[contains(text(), concat(\'You can\', "\'", \'t have an "empty" list item\'))]'
Run Code Online (Sandbox Code Playgroud)
XPath引擎看到它的方式(即内存中的实际字符串):
//span[contains(text(), concat('You can', "'", 't have an "empty" list item'))]
Run Code Online (Sandbox Code Playgroud)
lxml库允许XPath变量,这比那更优雅,但我怀疑Selenium find_elements_by_xpath支持它们.
| 归档时间: |
|
| 查看次数: |
126 次 |
| 最近记录: |