Xpath/按文本搜索输入节点

use*_*831 2 xpath webdriver selenium-webdriver

我有一个搜索框(<input>元素),我尝试通过写入其中的文本来获取元素。

我尝试做下一个:

// input [@value= "my text"] 
Run Code Online (Sandbox Code Playgroud)

但它不起作用(只是要清楚 - 该值不是输入元素的属性。accully,当我检查元素时,我看不到在此文本框中写入的文本)。

<div class="EwdOOOOO">
 <div class="GOOOEOOO">Hello </div> 
 <input type="text" class="GOBLEOOO"> 
</div>
Run Code Online (Sandbox Code Playgroud)

和我放的座椅盒:

"How are you?"
Run Code Online (Sandbox Code Playgroud)

您可以看到"How are you?"在 DOM 中没有找到。

谢谢。

e1c*_*che 5

你确定得到你的输入并填充它吗?

喜欢 :

WebElement input = driver.findElement(By.xpath("//input[@class='GOBLEOOO']"));
input.sendKeys("How are you?");
Run Code Online (Sandbox Code Playgroud)

所以你可以像这样得到你的元素:

WebElement inputByValue = driver.findElement(By.xpath("//input[@value='my text']"));
Run Code Online (Sandbox Code Playgroud)

或者你有另一种方式来做到这一点

WebElement inputByValue= driver.findElement(By.xpath("//input[contains(@value,'my text')]"));
Run Code Online (Sandbox Code Playgroud)

contains()如果您的属性值@value包含下一个参数(字符串),则返回 true

因为总是有一个value attributein ,input因为你输入了值。 这里有更多的输入规范。


如果您想找到具有您在输入字段中键入的值的元素,您可以使用此 xpath。

// you get the value you typed in the input
String myValue = driver.findElement(By.xpath("//input[@class='GOBLEOOO']")).getAttribute("value"); 

//you get a element that contains myValue into his attributes
WebElement element = driver.findElement(By.xpath("//*[contains(@id ,myValue) or contains(@value, myValue) or contains(@name, myValue)]"));
Run Code Online (Sandbox Code Playgroud)