2

I have a search box (<input> element), and I try to get the element by the text that written into it.

I try to do the next:

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

but it doesn't work (just to be clear - the value is not attribute of the input element. accully, when I do inspect element, I don't see the text that written in this text box).

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

and the seatch box I put:

"How are you?"

You can see that "How are you?" isn't found in the DOM.

Thanks.

e1che
  • 1,241
  • 1
  • 17
  • 34
user2251831
  • 41
  • 1
  • 1
  • 4

2 Answers2

5

Are you sure to get your input and populate it ?

like :

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

So you can get your element like that :

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

or you have another way to do that

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

contains() return true if the value of your attribute @value contains the next parameter (string)

Because there is always a value attribute in an input since you type values in. Here for more Input specs.


If you want to found an element with the value you typed in the input field you can use this 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)]"));
e1che
  • 1,241
  • 1
  • 17
  • 34
  • Thanks, but @value is not attrubute in my input element, as you can see – user2251831 Apr 23 '13 at 08:33
  • I dont want to sendKey, I want to find other element, and I need the text into the textbox for that (get, not set) – user2251831 Apr 23 '13 at 08:48
  • And there is no value in this input. the input is search box, not button – user2251831 Apr 23 '13 at 08:51
  • Thank you, but I think you don't understand my question. Even Id I wait 10 minutes, The value attribute would not be there! I have search box, in this box I type something. After it (even if I sleep 10 minutes), I want to find this element with the text I insert. – user2251831 Apr 23 '13 at 09:17
  • @user2251831, I'm confused, so you never send keys to the input, then why would be text in that input? Is text just a placeholder like the search box at the top right of this stackoverflow page? Or you somehow type the text in before and want to locate the input now? – Yi Zeng Apr 23 '13 at 09:17
  • @e1che: the [documentation](http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits) explains it well. Either replace `sleep(10);` with implicitlyWait, as one implicitlyWait will set all timeout for all findElement calls, or put `return new WebDriverWait(driver, i).until(ExpectedConditions.presenceOfElementLocated(By.id("someid")));` into the method – Yi Zeng Apr 23 '13 at 09:22
  • Yes, text just a placeholder like the search box at the top right of this stackoverflow page – user2251831 Apr 23 '13 at 09:23
  • @user2251831 I seem to catch now what you really want. You have an input where you type a value in. And you'll search in all your DOM if there is an element's name/id/value with this value ? – e1che Apr 23 '13 at 09:31
  • Can you please tell me if webDriver support of what I want? I want to put text in the search box like 'blablabla', and then search it and find this element. All your suggestions aren't help me, because "blablabla" is not attribute or placeholder. I need this form of element searching for many reasons. – user2251831 Apr 23 '13 at 11:24
0

See my answer here. Sometimes the text value that you are setting is assigned as a "value" attribute of text box and not as "text"

WebElement input = driver.findElement(By.cssSelector("input[type='text']"));
input.sendKeys(enteredValue)
String retrievedText = input.getAttribute("value");
if(retrievedText.equals(enteredValue)){
 //do stuff
}
Community
  • 1
  • 1
nilesh
  • 14,131
  • 7
  • 65
  • 79