Selenium Extract the ID of an element found via text in Java -


i have set of cucumber tests wish execute using selenium , chrome. of test work fine. trying parametertize 1 particular step using test can name elements want , test find them.

the cucumber test looks this:

when go "inventory" / "inventory" application  , search "description" field "a"   

i able when statement parameterized easily, , statement giving me bit more trouble. page has several sets of tables consistent id scheme xpath header word "description" looks this:

//label[@id='m6a7dfd2f_ttrow_[c:2]_ttitle-lb'] 

and input field has xpath

//td[@id='m6a7dfd2f_tfrow_[c:2]-c']/input 

for reference immediate next input field, located below label room, has xpath

//td[@id='m6a7dfd2f_tfrow_[c:3]-c']/input 

so each label , input field have same id scheme , number following "c:" seems change. number same both label , it's corresponding input value. how scan page word "description" , extract id of element associated , truncate down number. have variables set hold rest , follows:

@when("^i search \"([^\"]*)\" field \"([^\"]*)\"$") public void i_search_the_field_for(string searchfield, string searchitem) throws throwable { string baseinputxpath = "//td[@id='m6a7dfd2f_tfrow_[c:"; string endinputxpath = "]-c']/input"; string elementnumber = "the return of sort of method based on searchfield";//todo driver.findelement(by.xpath(baseinputxpath + elementnumber + endinputxpath)).clear(); driver.findelement(by.xpath(baseinputxpath + elementnumber + endinputxpath)).sendkeys(searchitem); driver.findelement(by.id("search_button_img")).click(); 

if there way pull out number based on description text, super helpful.

use xpath search webelement word 'description' in label tag.

"//label[text(),'description']" or "//label[.='description']" or "//label[contains(text(),'description')]" 

use getattribute("id") id.


Comments