C# Selenium how can I implement click on button without id

Viewed 46236

C# Selenium how can I implement click on button without id

Here is the html:

<div class="fe-margin"> 
  <button class="btn btn-default" data-bind="click: $root.addParameter, enable: $root.selectedParameter() == null" type="button"/>
  Add parameter button
</div>
9 Answers

Try this Xpath -

var element = driver.FindElement(By.XPath("//button[@class ='btn btn-default']"));

element.Click();

Thanks

IWebElement btn = Firefox.FindElement(By.ClassName("btn")); btn.Click();

This should do it. I found that using a part of the ClassName does the job.

You can use button name or text:

driver.FindElements(by.TagName("button")).FirstOrDefault(ele=>ele.GetAttribute("Name").Equals("Submit")).click();

or, case-sensitive:

driver.FindElements(by.TagName("button")).FirstOrDefault(ele=>ele.GetAttribute("Name").ToLower().Equals("Submit")).click();

or, in your case:

driver.FindElements(by.TagName("button")).FirstOrDefault(ele=>ele.Text.Equals("Name").Click();
Related