Reputation: 11
I have this button:
<div class="card-footer text-right">
<input id="login" type="submit" class="btn btn-sm btn-primary" value="Log In" />
And have written the code to click it as:
driver.findElement(By.xpath("//div[contains(@class,'btn btn-sm btn-primary')]")).click();
which does not work. Any help ?
Upvotes: 1
Views: 13742
Reputation: 193088
It is recommended that whenever an element is present in the HTML DOM for better performance we should try to access those elements through their respective locators id
or name
first. Next we should try linkText
,tagName
,css
and xpath
As per the element in your code, you can try these options:
driver.findElement(By.id("login")).click();
OR
driver.findElement(By.xpath("//input[@id='login']")).click();
OR
driver.findElement(By.xpath("//div[@class='card-footer text-right']/input[@id='login']")).click();
Now, as you are seeing ElementNotVisibleException
we will introduce an ExplicitWait for the element to be visible then try to click on it as follows:
WebElement myElement = (new WebDriverWait(driver, 15))
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='card-footer text-right']/input[@id='login']")));
myElement.click();
Let me know if this helps you.
Upvotes: 2
Reputation: 3384
Try following :
WebElement yourElement = driver.findElement(By.cssSelector("div.card-footer.text-right input#login"));
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOf(yourElement));
yourElement .click();
Upvotes: 0
Reputation: 1233
Maybe the login button is not visible or clickable when your script runs. Try adding a WebDriver wait. For example:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//div[@class='card-footer text-right']/input[@id='login']"));
driver.findElement(By.xpath("//div[@class='card-footer text-right']/input[@id='login']"))
.click();
Upvotes: 2
Reputation: 73
It seems div here won't stand in the way,
driver.findElement(By.xpath("//input[@id='login']")).click();
So, what this is doing is, it is looking for input tags all over the page and it doesn't matter what ancestors any of them may have. Then it selects the element which has 'login' as the value across the id attribute.
Upvotes: 0