arm
arm

Reputation: 451

Selenium Webdriver finding an element (a button)

I need my script to click on a button. So far I have tried this-

 driver.findElement(By.id("button_click")).click();

and HTML for this button is-

    <td class="Button">
<input class="btn" value="Click here to get started" name="button_click" onclick="this.form.action = '/servlet/servlet.Integration?lid=0076878676545487SVD2&eid=465124652J9ly&ic=1&retURL=%24848676854684y&wrapMassAction=1&scontrolCaching=1&linkToken=gituuiyiiuhjgd46etfjgioyyo8yo8ylihvTDNfLWhzdm5KLFlXWmtNR0po'; this.form.onsubmit = function() { return true }" title="page title" type="submit"/>
</td>

but it is not identifying the button right.

Upvotes: 4

Views: 52

Answers (2)

Gabriel Abel
Gabriel Abel

Reputation: 21

You can use

driver.findElement(By.Name("button_click")).click();

or

driver.findElement(By.CssSelector("input.btn:nth-of-type(X)")).click();

there is probably more than one input with 'btn' class in your page, so X would represent wich one of these buttons you want.

Upvotes: 1

Lajos Arpad
Lajos Arpad

Reputation: 76436

You try to search for your button by id, but your button has no id. The solution is to add an id to the button with the corresponding value:

    <td class="Button">
<input class="btn" value="Click here to get started" id="button_click" name="button_click" onclick="this.form.action = '/servlet/servlet.Integration?lid=0076878676545487SVD2&eid=465124652J9ly&ic=1&retURL=%24848676854684y&wrapMassAction=1&scontrolCaching=1&linkToken=gituuiyiiuhjgd46etfjgioyyo8yo8ylihvTDNfLWhzdm5KLFlXWmtNR0po'; this.form.onsubmit = function() { return true }" title="page title" type="submit"/>
</td>

If this is not an option, then you can search for elements by name, but then a list will be returned and you will need to choose the correct element from there, which is pretty simple if there is a single element.

Upvotes: 3

Related Questions