Selenium: submit() works fine, but click() does not

前端 未结 2 1911
感情败类
感情败类 2020-12-12 04:21

I have submit button, which is only one on the page, and it\'s in form.

html part:

2条回答
  •  [愿得一人]
    2020-12-12 05:03

    The method object.submit() is there for submitting the form to the server. It has another advantage, in case if you're not able to locate the "submit" button then you can take any object of the form and trigger submit() function. It seems in searchButton.submit(); searchButton is an element of the form and the submit action on it triggers submission of form on server.

    Now, why searchButton.click(); not working here could have following reasons.

    1. The button is visible but not enabled.
    2. Driver is finding the 2 instances of searchButton element.

    Suggestion: Evaluate following code and check it returns more than one element. If it does then you're clicking on the wrong instance.

    List e = driver.findElements(By.xpath("//button[@type='submit']"));
    

    Also try,

    driver.findElement(By.xpath(".//button[@class='btn btn__primary'][@type='submit']")).click()
    

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

提交回复
热议问题