我有一个搜索框( <input>
元素),我尝试通过写入其中的文本来获取该元素。
我尝试做下一步:
// input [@value= "my text"]
但它不起作用(只是要清楚 - 该值不是输入元素的属性。准确地说,当我检查元素时,我没有看到此文本框中写入的文本)。
<div class="EwdOOOOO">
<div class="GOOOEOOO">Hello </div>
<input type="text" class="GOBLEOOO">
</div>
还有我放的座位箱:
"How are you?"
您可以看到"How are you?"
在 DOM 中找不到。
谢谢。
最佳答案
您确定获取输入并填充它吗?
喜欢:
WebElement input = driver.findElement(By.xpath("//input[@class='GOBLEOOO']"));
input.sendKeys("How are you?");
所以你可以像这样得到你的元素:
WebElement inputByValue = driver.findElement(By.xpath("//input[@value='my text']"));
或者你有其他方法可以做到这一点
WebElement inputByValue= driver.findElement(By.xpath("//input[contains(@value,'my text')]"));
contains()
如果属性 @value
的值包含下一个参数(字符串),则返回 true
因为自从您输入值后,input
中始终存在一个value 属性
。
Here for more Input specs.
如果您想查找具有您在输入字段中键入的值的元素,您可以使用此 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)]"));
关于Xpath/按文本搜索输入节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16164334/