问题
How can i click on submit button on the given html
<span class="sbt-btn-wrap relative">
<input class="submit icon" type="submit" onclick="submitmnLogin($(this), 'testPrepLogin');trackGaEvent('Content Hub Home','Login Popup','Login Button');">
I have tried
driver.findElement(By.className("sbt-btn-wrap relative")).click();
and
driver.findElement(By.className("submit icon")).click();
but it is not working.
回答1:
When you have two class names with space separating them, then you cannot use By.className
on it. Instead use By.cssSelector
to click on the element. Here's how -
driver.findElement(By.cssSelector(".submit.icon")).click();
If you still want to use className then use one class name that is unique to the element and click on it -
driver.findElement(By.className("submit")).click();
You can use other attributes to click the input element. I prefer using cssSelector instead of xpath as its slow -
driver.findElement(By.cssSelector("input[type='submit']")).click();
Here's an example of xpath too -
driver.findElement(By.xpath("//input[@type='submit']")).click();
If you are not able to find submit button element in a unique way then use other unique element to find input element.
driver.findElement(By.cssSelector(".sbt-btn-wrap relative .submit")).click();
Hope it helps.
回答2:
There is a couple of way's using this HTML code for the button
<input type="button" id="btn_submit" class="some_class" value="Submit">
And use the following jquery code (you must include jquery on you page!)
$('#btn_submit').click(function() {
alert( "Handler for .click() called." );
});
Or performing the same still using jquery, but handle the click with the class and not by ID (recommended only if you want to handle the same on multiple elements)
$('.some_class').click(function() {
alert( "Handler for .click() called." );
});
Now with javascript by ID no need to include the jquery library
function myFunction() {
document.getElementById("btn_submit").click(); // Click on the checkbox
}
And by css
driver.findElement(By.cssSelector(".some class)).click();
Hope this is what you want and this helps you. Check the first comment on your question too
回答3:
You can achieve this by help of CSSSelector using of List web element
String cssSelectorOfButton="input[type='button'][id='login']";
//****Add cssSelector of your button webelement
List<WebElement> loginButton
=driver.findElements(By.cssSelector(cssSelectorOfButton));
loginButton.get(0).click();
回答4:
replace .click()
with .submit()
and it may work. You could be working with an HTML form.
来源:https://stackoverflow.com/questions/32779563/how-can-i-click-submit-button