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

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

问题描述

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

html 部分:

<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">选择我的车

</表单>

所以,我正在使用 xpath:

//button[@type='submit']

我通过 submit() 成功按下了它(让我跳过 WebDriver 初始化,它很好):

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

(和一些搜索执行)

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

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

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

@Test公共无效测试(){WebElement button = driver.findElement(By.xpath("//button[@type='submit']"));button.click();}

请有人解释一下,为什么 submit() 在这种情况下成功按下按钮,但 click() - 没有.我不明白,为什么测试"是绿色的,当我们尝试点击()时,但如果查看驱动程序启动的浏览器,它没有执行.

更新:我试过

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

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

但还是一样 - submit() 工作正常,click() - 没有.

解决方案

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

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

  1. 该按钮可见但未启用.
  2. 司机正在寻找 2searchButton 元素的实例.

建议:评估以下代码并检查它返回多个元素.如果是,那么您点击了错误的实例.

Liste = 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

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.

解决方案

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

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

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆