Selenium Webdriver submit() vs click()
Asked Answered
P

8

69

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() ?

Pythian answered 8/7, 2013 at 15:10 Comment(0)
P
90

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

Pomp answered 8/7, 2013 at 15:35 Comment(5)
ah, so there is no difference of execution. I've figured that much. The freezing must occur from another source/IE glitch...really dislike IE + selenium. Thank you for the answer.Pythian
That's technically not true - the difference is one will fire the click event on the input element of type submit and the other will fire the submit event on the form element. If you have code that only fires on the click event of the input then this will not occur when you use the submit function.Oedema
There is also one important difference, at least for the .net version using chromedriver, submit will wait for the page to load, click is not garanteed.to waitCotonou
I think element.submit() only works elements are located by find_element_by_name() and not by ID, at least this happens with meGracia
selenium submit is deprecated selenium.dev/documentation/webdriver/elements/interactions/…Rosenda
F
41

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.

Fearless answered 20/1, 2014 at 15:49 Comment(1)
That's is. Most of modern UI's does fire javascript functions while clicking almost every button due to extensive usage of jQuery. webdriver.submit() is more like a "form2url" function. This is the why it can be fired using any other elements, including outside the form because it's nothing related with the webelement at all, just getting values from the first form instance. If you wanna fire a form that is not the first one you should get first the form element or remove others.Bravin
T
15

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.

Trunnion answered 22/4, 2014 at 23:6 Comment(4)
Confirmed. Had an issue with submit() on the submit button, but it wouldn't bring through the value attribute of the button. using click() on the button worked like a charm.Kenneth
Thank you , thank you, thank you. Not sure why submit isn't working though.Genniegennifer
Same problem here. My code became convoluted, because now I have to wait the page to load.Pauiie
Ditto. Thanks for the comment. Helped a ton to verify what I was seeing when using submit().Phillipphillipe
D
9

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

Diazole answered 20/1, 2014 at 19:27 Comment(2)
Today, I had to replace click call with submit because click does not wait anymore for page to load, I am sure click was working correctly. It must be something has been changed recently, but i can't prove it. As always with selenium if thing are working, it doesnt mean they are done correctly.Cotonou
I don't think this is true. artoftesting.com/automationTesting/…. An element that triggers page load waits till the DOM gets loaded before returing control to the driver. For example - if we submit a form by clicking the Submit button then the next statement after the submit button click operation will be attemted by webDriver only after the page gets loaded completely. So, in most cases we don't even have to wait for the page to load. "In conclusion, wait for page load should not be needed. Selenium does this automatically.Phillipphillipe
M
2

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.

Maloney answered 15/6, 2016 at 11:12 Comment(0)
X
2

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.

Xylidine answered 28/3, 2017 at 8:46 Comment(0)
F
1

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 
For answered 2/7, 2018 at 10:11 Comment(0)
B
-1

element.click() method works for all types of

  • button
  • link
  • checkbox
  • combobox

whereas submit() - The target of the submit() method must be a element within the HTML markup of the page or any of its child elements. With this method, you don’t need to explicitly target the form’s submission button — you’re simply submitting the form

It will also work with button/link but exception will throw "java.lang.UnsupportedOperationException"

validated and reference link - https://makersaid.com/submit-vs-click-selenium/

Example - code inspect

Here I haven't captured the "Login" button element, I just used last element which is Password then used submit and it worked as enter without any error.

selecnium code

Batter answered 23/12, 2023 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.