3
votes

Working on creating automated tests with selenium I expired problem when I try to locate text input by text inside it. I use Xpath and FirePath plugin for checking it's result.

My xpath is:

//input[@context='@name' and contains(@class, 'edit-string') and contains(@value, 'testSection')]

and it returns nothing, while when I remove last condition:

//input[@context='@name' and contains(@class, 'edit-string')]

i found required div.

As I undestand the problem is in that fact that value attribute doesn't contains typed text. Firebug console proves this theory(jQuery library present on the page):

>>>> $("input.edit-string")[0].getAttribute("value")
null
>>>> $("input.edit-string")[0].value
"testSection"
>>>> $("input.edit-string")[0].getAttribute("context")
"@name"

Any ideas how can I use typed text in xpath expression?

2
Could you please post the xml containing the <div> element you're looking for? - JWiley
Actually, I just successfully tried to look for an input element by text. What is your Selenium version (also, Selenium RC or Selenium 2?) and Firefox version? Could you post a whole code for us to see whether there is a problem in something else or not? Did you try Implicit/Explicit wait? - Petr Janeček
I use selenium with chrome driver and it fails to locate element using specified xpath. The same result when using FirePath. So it is not depends on selenium code, as firepath return same result. Now about input element - it's created dynamically on page load and it hasn't explicit value attribute in it. To repdoduce just write this in html:$("body").append($("<input type='text' id='asd'>"));$("#asd").val("asd"); and then use this xpath: //input[@value='asd'] - Dmitry
to JWiley. I am not looking for a <div>, i am looking for <input> element and for some reason the only sufficient condition for selecting element is text typed inside field, so it doesn't metter what xml is around. - Dmitry
@Dmitry Aha! Yeah, that doesn't work. I'm going to investigate further tommorrow, but here's a quick thought: What is the behaviour when you set the value of input by 'setAttribute("value", "someValue")'? Also, if this problem turns up unsolvable, you can work around by checking via JS, too. (JavascriptExecutor)driver.executeScript('thetest()') - Petr Janeček

2 Answers

0
votes

Please try it with following instructions..

Command ---> Target ---> Value
focus --->class=edit-string
typeKeys --->class=edit-string--->any text

0
votes

Approach: From the Given Info the XPath is //input[@context='@name' and contains(@class, 'edit-string')]

Step 1:

So the Input is having the Class Name that contains the string "edit-string". We can convert the above XPath into CSS selector

css=.edit-string

Step 2:

Try getting the Text

$(".edit-string").getText();