如何在 XPath 中选择一个空元素? [英] How do I select an empty element in XPath?

查看:25
本文介绍了如何在 XPath 中选择一个空元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 Django 1.4 测试和 Selenium 选择元素,如下所示:

We select elements with Django 1.4 tests and Selenium like this:

self.assertEqual(1, len(self.selenium.find_elements_by_xpath("//a[@href='#'][@class='button save-as'][@title='Save As...'][text()='Save As']")))

(该类继承自 LiveServerTestCase).

问题是有时会有没有文本的元素,如果我们使用 [text()=''] 选择它会失败(len 为 0).如何选择没有文本的元素?

The problem is that sometimes there are elements without text, and if we select with [text()=''] it fails (the len is 0). How can I select elements without text?

更新:因为 [text()=''] 不起作用,我不得不断言两行来断言没有文本:

Update: Because [text()=''] didn't work, I had to assert two lines to assert no text:

self.assertEqual(1, len(self.selenium.find_elements_by_xpath("//a[@href='#'][@class='button properties'][@title='Properties']")))
self.assertEqual("", self.selenium.find_element_by_xpath("//a[@href='#'][@class='button properties'][@title='Properties']").text)

现在我可以用一行来断言:

Now I can assert the same with one line:

self.assertEqual(1, len(self.selenium.find_elements_by_xpath("//a[@href='#'][@class='button properties'][@title='Properties'][not(text())]")))

推荐答案

你可以使用 XPath 的 not() 函数.

You could use XPath's not() function.

//a[@href='#'][@class='button save-as'][@title='Save As...'][not(text())]

这篇关于如何在 XPath 中选择一个空元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆