具有 ID 并包含文本的 Xpath 表达式

use*_*141 3 java selenium xpath webdriver

所以我想使用 Xpath 查找以下标签:

 <h1 id="label">This is the text</h1>
Run Code Online (Sandbox Code Playgroud)

通常,我会这样写:

//h1[@id='Label' and text() = "This is the text"]
Run Code Online (Sandbox Code Playgroud)

或者,如果我只想提取文本的一部分,我可以使用以下命令:

//h1[text()[contains(.,'text')]]
Run Code Online (Sandbox Code Playgroud)

现在,我尝试寻找教程是否可以混合这两个语句:所以我想要一个既包含 @id 又包含文本“Text”的 Xpath 表达式。这样的事有可能吗?

我尝试这样做:

//h1[@id='Label' and [contains(@text,"text"])] 
Run Code Online (Sandbox Code Playgroud)

但这并没有给我一个正确的结果。

And*_*kov 6

代替:

//h1[@id='Label' and [contains(@text,"text"])] 
Run Code Online (Sandbox Code Playgroud)

和:

//h1[@id='label' and contains(text(),'text')] 
Run Code Online (Sandbox Code Playgroud)