Selenium:submit()工作正常,但click()没有 [英] Selenium: submit() works fine, but click() does not

查看:602
本文介绍了Selenium:submit()工作正常,但click()没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有提交按钮,它只是页面上的一个按钮,而且它是在表单中。

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

html part:

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>

所以,我正在服用xpath:

So, I'm taking xpath:

//button[@type='submit']

我通过submit()成功按下它(让我跳过WebDriver init,它很好):

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

(以及一些搜索执行)

但是,当我试图通过click()按下它时,

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

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

它没有在启动的浏览器中按下,同时Junit测试为绿色(不测试,但是只需按下按钮):

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();
    }

可以请有人解释,为什么在这种情况下提交()成功按下按钮,但是点击() - 不。而且我不明白,为什么test是绿色的,当我们尝试点击()时,如果查看由驱动程序启动的浏览器则没有执行。

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.

更新:
我试过

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

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

但仍然相同 - submit()工作正常,点击() - 没有。

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

推荐答案

方法 object.submit()用于将表单提交给服务器。它有另一个优点,如果你无法找到提交按钮,那么你可以获取表单的任何对象并触发submit()函数。
似乎在 searchButton.submit(); searchButton是表单的一个元素,对它的提交操作会触发在服务器上提交表单。

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.

现在,为什么 searchButton.click(); 在这里工作可能有以下原因。

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


  1. 该按钮可见但未启用。

  2. 驱动程序正在查找 searchButton 元素的2
    个实例。

  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']"));

也试试,

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

这篇关于Selenium:submit()工作正常,但click()没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆