Reputation: 2527
Does a button need to be in a form inorder to perform click operation on it? I am using
driver.findElement(By.tagName("button")).submit();
I also tried targeting that button with classname but I keep getting the below error.
Element was not in a form so couldn't submit Command duration or timeout: 0 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Upvotes: 0
Views: 1183
Reputation: 29082
Does a button need to be in a form inorder to perform click operation on it?
No. Nothing "Has" to be anywhere to operate on it as long as it's visible.
but I keep getting the below error
That's because you are trying to invoke submit()
on that button. If it WERE in a form this would work, but since it is not, that is why you are getting that error.
I also tried targeting that button with classname
How you select it is irrelevant. You are most likely finding the right one unless there are more than 1 <button />
element. Some alternatives would be,
By.cssSelector('button[attr='attr']')
By.tagName('button') // this is assuming that it is the only button in the DOM.
By.className('someClass')
Do as @Richard had mentioned and invoke the click()
method rather than the submit()
method.
Upvotes: 2
Reputation: 9029
You should be able to use driver.findElement(By.tagName("button")).click()
Upvotes: 1