Ranjana
Ranjana

Reputation: 31

selenium webdriver -xpath generation

I am working in selenium webdriver.I have few text boxes whose ids are going to change all the time.

e.g id=ctl00_SPWebPartManager1_g_ad39b78c_a97b_4431_aedb_c9e6270134c6_ctl00_wizNotification_ucChangeData_txtAddress1

but last part remains same always. in above example wizNotification_ucChangeData_txtAddress1 i have tried to go with xpath like:

//input[contains(@id,'txtAddress1')
//input[ends-with(@id,'txtAddress1')]

but while running not able to identify the textarea.

Any suggestions please. I tried as well with: //input[ends-with(@id,'wizNotification_ucChangeData_txtAddress1')] but no Luck :(

Upvotes: 3

Views: 4868

Answers (3)

Krish13287
Krish13287

Reputation: 198

In case of webelements with dynamic Ids, instead of going for Xpath with Ids we can go for other way of finding elements like 'by tagname', CSSlocator,.. it worked for me.

Upvotes: 0

nilesh
nilesh

Reputation: 14279

Xpaths are slow in IE because IE does not have native Xpath engine. You should instead use CSS Selector for better performance. As for your case, you can try below css selector which finds an input for which the id ends with txtAddress1

E[foo$="bar"] an E element whose "foo" attribute value ends exactly with the string "bar"

WebElement element = driver.findElement(By.cssSelector("input[id$='txtAddress1']"));

Upvotes: 3

jasalguero
jasalguero

Reputation: 4181

Try:

.//input[@id[contains(.,'txtAddress1')]]

Be careful, if is a textarea it won't be detected as an input.

Upvotes: 1

Related Questions