Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

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

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

html part:

<form class="search-form ng-touched ng-dirty ng-valid" novalidate="" style="" xpath="1">

<div class="row">...</div>
<div class="row">...</div>
<div class="row">...</div>
<div class="form__actions" xpath="1">
    <div class="form__buttons">
      <!---->
      <div class="btn__wrapper">
        <button class="btn btn__primary" type="submit">
          Select My Car
        </button>
      </div>
    </div>
  </div>

</form>

So, I'm taking xpath:

//button[@type='submit']

I'm successfully pressing it via submit() (let me skip WebDriver init, its fine):

  WebElement searchButton = driver.findElement(By.xpath("//button[@type='submit']"));
  searchButton.submit();

(and some search performs)

But when I'm trying to press it via click()

WebElement searchButton = driver.findElement(By.xpath("//button[@type='submit']"));
        searchButton.click();

it's not pressed in browser which is launched, and same time Junit test is green (not test, but just pressing button):

@Test
    public void test() {
        WebElement button = driver.findElement(By.xpath("//button[@type='submit']"));
        button.click();
    }

Can please someone explain, why submit() successfully presses button in such case, but click() - no. And I do not understand, why "test" is green, when we are trying to click(), but it was not performed, if looking on browser launched by driver.

UPDATED: I tried

WebElement button = driver.findElement(By.xpath("//button[@type='submit']"));
        if (button.isEnabled()) {
            button.click();
        }

and

WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(button)).click();

but still the same - submit() works fine, click() - does not.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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<WebElements> 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...