Selenium Webdriver submit() vs click()

JavaSeleniumInternet ExplorerSelenium WebdriverSubmit

Java Problem Overview


Let's say I have an input in a form (looks like a button and interacts like a button) which generates some data (well, the server generates the data based on the form parameters, but for the user, the button does it :) )based on the parameters in the form.

When I use click(), the whole process hangs (it actually freezes, no exceptions or errors).

From the Selenium website:

// Now submit the form. WebDriver will find the form for us from the element
element.submit();

So WebDriver has a submit() method. Is there any difference, logic wise, between using a click() on a button or submit() ?

Java Solutions


Solution 1 - Java

The submit() function is there to make life easier. You can use it on any element inside of form tags to submit that form.

You can also search for the submit button and use click().

So the only difference is click() has to be done on the submit button and submit() can be done on any form element.

It's up to you.

http://docs.seleniumhq.org/docs/03_webdriver.jsp#user-input-filling-in-forms

Solution 2 - Java

There is a difference between click() and submit().

submit() submits the form and executes the URL that is given by the "action" attribute. If you have any javascript-function or jquery-plugin running to submit the form e.g. via ajax, submit() will ignore it. With click() the javascript-functions will be executed.

Solution 3 - Java

I was a great fan of submit() but not anymore.

In the web page that I test, I enter username and password and click Login. When I invoked usernametextbox.submit(), password textbox is cleared (becomes empty) and login keeps failing.

After breaking my head for sometime, when I replaced usernametextbox.submit() with loginbutton.click(), it worked like a magic.

Solution 4 - Java

Also, correct me if I'm wrong, but I believe that submit will wait for a new page to load, whereas click will immediately continue executing code

Solution 5 - Java

Neither submit() nor click() is good enough. However, it works fine if you follow it with an ENTER key:

search_form = driver.find_element_by_id(elem_id)
search_form.send_keys(search_string)
search_form.click()
from selenium.webdriver.common.keys import Keys
search_form.send_keys(Keys.ENTER)

Tested on Mac 10.11, python 2.7.9, Selenium 2.53.5. This runs in parallel, meaning returns after entering the ENTER key, doesn't wait for page to load.

Solution 6 - Java

submit() method can be used to click on the button present in the form and Type attribute should be "submit".

click() method is used to click on the button in the webpage.

Correct me if i am wrong.

Solution 7 - Java

click() - Perform only click operation as like mouse click.

submit() - Perform Enter operation as like keyboard Enter event.

For Example. Consider a login page where it contains a username and password and submit button.

On filling password if we want to login without clicking login button. We need to user submit button on the password where click() operation does not work (to login into an application).

driver.get("https:// anyURL"); 
driver.manage().window().maximize(); 
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
driver.findElement(By.id("txtUserId")).sendKeys("[email protected]"); 
WebElement text = driver.findElement(By.id("txtPassword")); text.sendKeys("password"); 
Thread.sleep(1000); 
text.click();   //This will not work - it will on perform click operation not submit operation
text.submit(); //This will perform submit operation has enter key 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCosminOView Question on Stackoverflow
Solution 1 - JavaDan2.0View Answer on Stackoverflow
Solution 2 - JavaIonTichyView Answer on Stackoverflow
Solution 3 - Javauser3000430View Answer on Stackoverflow
Solution 4 - Javauser2426679View Answer on Stackoverflow
Solution 5 - JavamanpurView Answer on Stackoverflow
Solution 6 - JavaGowthamView Answer on Stackoverflow
Solution 7 - JavaSantosh KanekarView Answer on Stackoverflow