Moe Moe - 1 year ago 92
Java Question

How to click on an input type submit with no defined ID using Selenium WebDriver and Java

I am trying to login to website, the login form doesn't have any ids associated with its value, and it uses only names:

<div id="warp">
<div id="content">
<form action="/index.php" method="post" name='login'>
username: <input type="text" name="username">
password: <input type="text" name="password">
<input type="submit" name="login" class="submit" value="login">

using the following commands I am able to fill in the user ID and password, but I can't click submit.

WebElement username = driver.findElement("username"));
WebElement password = driver.findElement("password"));

this doesn't get executed:

WebElement login = driver.findElement("login"));;


Unable to locate element login

And if I did element.submit();
it clears out the entered data in the pass/user fields.

Answer Source

As I'm seeing in your provided HTML, there is two elements present with same name login, you need to create more specific your locator to locate login button using By.cssSelector() as below :-

new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.submit[name = 'login']"))).click();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download