1

I found a few answers about this but they did not answer my question an so I am writing a new question.

I have HTML code having below kind of checkbox elements (in browser's inspect element)

<input role="checkbox" type="checkbox" id="jqg_TransactionFormModel501EditCollection2_147354_grid_-1274" class="cbox" name="jqg_TransactionFormModel501EditCollection2_147354_grid_-1274" value="true">

In my test case I want to click on checkbox using its ID using Selenium Webdriver.

here Id= "jqg_TransactionFormModel501EditCollection2_147354grid-1274" is dynamic.

in above id, Bold & Italic marked letters (dynamic) will change with different check boxes in same page as well as page refresh.

Bold marked letters (dynamic) will change on page refresh only (remain same through all the check boxes in same page.)

How shoud I format/write XPATH so that I can click on desired check boxes using below statement.

WebElement checkbox = webDriver.findElement(By.id("idOfTheElement"));
if (!checkbox.isSelected()) {
    checkbox.click();
}

Thanks for your help in advance.. !

R Dhaval
  • 536
  • 1
  • 9
  • 21
  • people who give negative vote to this question, please help me improve the question. and if you don't want to help then please don't vote it and let others help me. – R Dhaval May 23 '17 at 09:40
  • If any below answer is helpful to you the please accept or upvote else still facing issue then raise your query – Trimantra Software Solution May 23 '17 at 11:53
  • @TrimantraSoftwareSolution I will accept once I get answer to my 'raised query'. Thanks for your suggestion though :) – R Dhaval May 23 '17 at 12:00

4 Answers4

1

Here are a few examples of xpaths which you can use to find your checkbox

  //input[contains(@id,'jqg_TransactionFormModel')]

OR, if you want more checks, try something like

  //*[starts-with(@id,'jqg_TransactionFormModel') and contains(@id,'EditCollection2_')]

Additionally, you can try regex as well using matches

  //*[matches(@id,'<regex matching your id>')]
Anshuman
  • 831
  • 5
  • 18
  • Thank you!! I am interested in using regex that you suggested. Can you help me how to make regex for above mentioned ids. – R Dhaval May 23 '17 at 11:53
  • I had to do some searching and found this... Unfortunately, you cannot use matches in Selinium: https://stackoverflow.com/questions/17000703/is-string-matches-supported-in-selenium-webdriver-2 – Anshuman May 23 '17 at 13:03
0

You can use partial ID using cssSelector or xpath

webDriver.findElement(By.cssSelector("[id*='TransactionFormModel']"));

webDriver.findElement(By.xpath("//input[contains(@id, 'TransactionFormModel')]"));

You can replace TransactionFormModel with any other fixed part of the ID.

As a side note, no need to locate the element twice. You can do

WebElement checkbox = webDriver.findElement(By.id("idOfTheElement"));
if (!checkbox.isSelected()) {
    checkbox.click();
}
Guy
  • 46,488
  • 10
  • 44
  • 88
0

You can write xpath like this :

//input[starts-with(@id,'jqg_TransactionFormModel')]

//input[contains(@id,'jqg_TransactionFormModel')]
SaiPawan
  • 1,189
  • 7
  • 20
0

I recommend not using ID's or xpath and adding a data attribute to your elements. This way, you can avoid the annoyance of xpath and have a strong selector that you feel confident will always be selectable.

For example, you could call the attribute: data-selector. You can assign a value of "transactionFormModelCheckbox". Then, when creating a new element, you create by css selector with the value referenced above.

No matter what type of formatting, you'll always be able to select that checkbox - as long as the element exists in the DOM. If you need to investigate other attributes, you can use the selector to do things like hasClass() or anything else you need.

Hope that helps !

Kyle Gress
  • 46
  • 3