19

In my web page I'm having following two buttons:

<button onclick="addToSelected('newApplicationForm');">Add Strategy</button>

<button onclick="submitAddNewApplication('newApplicationForm');">Submit</button>

How do I identify them using methods of the By class?

0

4 Answers 4

34
By.xpath("//button[contains(.,'Add Strategy')]")
By.xpath("//button[contains(.,'Submit')]")

Notice that it is same as:

By.xpath("//button[contains(text(),'Add Strategy')]")
By.xpath("//button[contains(text(),'Submit')]")
2
  • 2
    Based on the html provided this is the best answer we could give. However, if more html were provided, we could probably find a better way to do it. Remember that xpath is one of the slower methods of finding items in selenium. Also at the risk of sounding like a broken record, I prefer not to use the inner text to identify elements since that gets localized and your automation would not run in localized builds, and the text is much more likely to change than an actual unique identifier in the markup.
    – Sam Woods
    Commented Feb 24, 2012 at 18:03
  • I would avoid using xpaths if possible. They are difficult to read and difficult to debug. Here is a solution without xpaths that should do the same as the first line of your answer. string buttonText = "Add Strategy"; var allButtons = driver.FindElements(By.TagName("button")); var button = buttons.First(button => button.Text == buttonText);
    – Silv
    Commented Oct 22, 2020 at 11:04
3

For Example...Login button is available on login screen...

//Click on Login

WebElement click = driver.findElement(By.xpath("//*[@id='btnLogOn']"));

click.click();
2

The xpath examples here will work - but you could also use a css selector to match inside the contents of the "onclick" attribute.

By.cssSelector("button[onclick^=addToSelected]")
By.cssSelector("button[onclick^=submitAddNewApplication]")

In this case I've used the "starts with" attribute selector, but you could use the "contains" selector instead (*=)

0

yes, as Tarun says, you can use xpath, but in our project, it's usually a required action for developer to add IDs to each elements. because,sometimes the UI or the text may change, then you had to modify your test script.

3
  • 2
    Tests of this sort should test the app the way a user would use it -- that is, they should test as much as possible based on UI, not element IDs. Therefore, they should find buttons based on text, not ID. Yes, that will require changing the test when the display text changes, but that's a good thing -- since the tests ensure that the UI is as desired, a UI change should break them. Commented Mar 4, 2015 at 4:46
  • I respectfully disagree with Marnen Laibow-Koser's statement. Testing based on the literal UI text leads to maintenance nightmares (been there, done that, it sucks). Validation of the text of a button, link, etc. should be a distinct test, not as a side effect of testing functionality. Finding objects on the page by literal text is also makes testing of pages translated to another language much more difficult. Commented Oct 6, 2016 at 19:05
  • This isn't about validation of the text, quite; rather, it's making sure the control is where the user can find and understand it. Remember, text is a UI element; classes are not. I know it may seem easier not to use the literal text, but that just leads to UI tests that don't actually test the UI. Commented Jan 15, 2018 at 16:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.