2013-04-23 47 views
1

我有一个搜索框(<input>元素),我试图通过写入它的文本来获取元素。Xpath /通过文本搜索输入节点

我尝试做了下:

// input [@value= "my text"] 

,但它不工作(仅仅是明确的 - 值属性不input元素accully,当我做检查元素,我不看不到写在这个文本框中的文字)。

<div class="EwdOOOOO"> 
<div class="GOOOEOOO">Hello </div> 
<input type="text" class="GOBLEOOO"> 
</div> 

和seatch盒,我把:

"How are you?" 

你可以看到,"How are you?"未在DOM中。

谢谢。

+0

您使用的硒? – e1che 2013-04-23 08:20:17

+0

@ e1che:是的,我可以用 – user2251831 2013-04-23 08:20:44

+1

来获取你的html吗? – e1che 2013-04-23 08:23:42

回答

3

你确定要得到你的意见并填充它吗?

喜欢:

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')]")); 

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

因为总有一个value attributeinput,因为你如果要找到与您可以使用此XPath输入字段中键入的值的元素类型的值。 Here for more Input specs.


// 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)]")); 
+0

它不起作用。我在我的问题中添加更多细节。 – user2251831 2013-04-23 08:29:45

+0

谢谢,但@value不是attrubute在我的输入元素,因为你可以看到 – user2251831 2013-04-23 08:33:43

+0

我不想sendKey,我想要找到其他元素,并且我需要文本到该文本框中(获取,未设置) – user2251831 2013-04-23 08:48:44

0

见我的回答here。有时,你要设置的文本值被指定为文本框中的“值”属性而不是“文本”

WebElement input = driver.findElement(By.cssSelector("input[type='text']")); 
input.sendKeys(enteredValue) 
String retrievedText = input.getAttribute("value"); 
if(retrievedText.equals(enteredValue)){ 
//do stuff 
}