[java] Selenium Webdriver submit() vs click()

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

The answer is


.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 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 password where .click() operation does not work.[to login into application]

Brif.

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 

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.


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


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.


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.


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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to selenium

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser Class has been compiled by a more recent version of the Java Environment How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? How to make Firefox headless programmatically in Selenium with Python? element not interactable exception in selenium web automation Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click How do you fix the "element not interactable" exception?

Examples related to internet-explorer

Support for ES6 in Internet Explorer 11 The response content cannot be parsed because the Internet Explorer engine is not available, or Flexbox not working in Internet Explorer 11 IE and Edge fix for object-fit: cover; "Object doesn't support property or method 'find'" in IE How to make promises work in IE11 Angular 2 / 4 / 5 not working in IE11 Text in a flex container doesn't wrap in IE11 How can I detect Internet Explorer (IE) and Microsoft Edge using JavaScript? includes() not working in all browsers

Examples related to selenium-webdriver

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? How to make Firefox headless programmatically in Selenium with Python? Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click How do you fix the "element not interactable" exception? Scrolling to element using webdriver? Only local connections are allowed Chrome and Selenium webdriver Check if element is clickable in Selenium Java

Examples related to submit

React - clearing an input value after form submit angular2 submit form by pressing enter without submit button spark submit add multiple jars in classpath jQuery's .on() method combined with the submit event Selenium Webdriver submit() vs click() PHP form - on submit stay on same page Disable submit button ONLY after submit HTML - How to do a Confirmation popup to a Submit button and then send the request? HTML form with multiple "actions" Javascript change color of text and background to input value