Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

On my web page I have the 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?

share|improve this question
    
I changed the wording a bit to clarify the question. –  user246 Feb 24 '12 at 13:38

4 Answers 4

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 (*=)

share|improve this answer
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')]")
share|improve this answer
1  
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 Feb 24 '12 at 18:03

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

//Click on Login

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

click.click();
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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