2013-04-23 44 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 
}