Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 一个Xpath表达式没有';在selenium中不起作用,但在Firefox中起作用_Html_Css_Xpath_Webdriver_Selenium Webdriver - Fatal编程技术网

Html 一个Xpath表达式没有';在selenium中不起作用,但在Firefox中起作用

Html 一个Xpath表达式没有';在selenium中不起作用,但在Firefox中起作用,html,css,xpath,webdriver,selenium-webdriver,Html,Css,Xpath,Webdriver,Selenium Webdriver,我有一个关于xpath的问题。 chrome中有这样的td: <td class="dataCol col02"> "Hello world(notes:there is$)nbsp;" <a href="xxxx">[View Hierarchy]</a> </td> ,它可以定位该元素 但当我使用SeleniumAPI2.24时,它找不到该元素 by.xpath("//td[text()='Hello world']") 你知道吗? 谢谢

我有一个关于xpath的问题。 chrome中有这样的td:

<td class="dataCol col02">
"Hello world(notes:there is$)nbsp;"
<a href="xxxx">[View Hierarchy]</a>
</td>
,它可以定位该元素

但当我使用SeleniumAPI2.24时,它找不到该元素

by.xpath("//td[text()='Hello world']")
你知道吗?
谢谢

尝试使用
normalize-space()
修剪前导和尾随空白字符:

//td[normalize-space(text())='Hello world']

编辑以下不同的注释:

下面是一个XPath表达式,它可能更适合一般情况:

//td[starts-with(normalize-space(.), 'Hello world')]

这意味着,如果整个
的串联字符串内容(不包括前导和尾随空格)以“Hello world”开头,它将匹配
节点。我将尝试使用
contains()
函数。
您的xpath看起来像:
//td[contains(text(),'Hello world')]

您的意思是有一个空格还是
实体?顺便说一句,检查源代码(Ctrl+U),而不是检查程序显示的DOM,例如Chrome
devTools
Firebug
,因为它们是源代码的编辑版本如果元素在
iframe
中,在选择中的元素之前,必须使用
driver.switchTo().frame()
。还要添加此-
//td[规范化空间(.)='Hello world']
。:)我认为@Babai,
//td[normalize space(.)='Hello world']
也会在测试中包含“[View Hierarchy]”字符串,因此它不会匹配
//td[以(规范化空格(.),'Hello world')开头]
有效,尽管我只是想告诉你,
text()
不需要。只有
可以工作。。我在这里要求的解决方案是你答案的更简洁版本。就这样
//td[normalize space(text())='Hello world']
可以写成
//td[normalize space(.)='Hello world']
。就这样DI agree
text()
通常不需要用于您发现的许多用例。但是
text()
在此上下文中并不等效:
是当前节点,在本例中为
Hello world
有两个节点,一个包含“Hello world”的文本节点和一个
Ohh!!我明白了……)现在就去拿吧……)
//td[normalize-space(text())='Hello world']
//td[starts-with(normalize-space(.), 'Hello world')]