title: Selenium Extract the ID of an element found via text in Java
body: <p>I have a set of Cucumber tests I wish to execute using selenium and Chrome. Most of my test work fine. I am currently trying to parametertize one particular step so that anyone using the Test can simply name the elements they want and the test will find them.</p>
  
  <p>The Cucumber test looks something like this: </p>
  
  <pre><code>When I go to the "Inventory" / "Inventory" application
  
  And I search the "Description" field for "a"  
  </code></pre>
  
  <p>I was able to get the when statement to parameterized very easily, but the And Statement is giving me a bit more trouble. The page itself has several sets of tables with a consistent id scheme The xpath for the header with the word "Description" looks like this: </p>
  
  <pre><code>//label[@id='m6a7dfd2f_ttrow_[C:2]_ttitle-lb']
  </code></pre>
  
  <p>and the input field has this xpath</p>
  
  <pre><code>//td[@id='m6a7dfd2f_tfrow_[C:2]-c']/input
  </code></pre>
  
  <p>For reference the immediate next input field, located below the label room, has this xpath</p>
  
  <pre><code>//td[@id='m6a7dfd2f_tfrow_[C:3]-c']/input
  </code></pre>
  
  <p>So each label and input field have the same id scheme and only the number following "c:" seems to change. That number is the same for both the label and it's corresponding input value. I would like to some how scan the page for the word "Description" and extract the ID of the element associated with it and then truncate it down to the number. I already have variables set up to hold the rest and it would hopefully as follows: </p>
  
  <pre><code>@When("^I search the \"([^\"]*)\" field for \"([^\"]*)\"$")
  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 some 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();
  </code></pre>
  
  <p>If there is a way to pull out that number based only on the description text, that would be super helpful. </p>