【问题标题】:Selenium: submit() works fine, but click() does notSelenium:submit() 工作正常,但 click() 不行
【发布时间】:2018-06-08 02:52:06
【问题描述】:

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

html部分:

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

//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
    public void test() {
        WebElement button = driver.findElement(By.xpath("//button[@type='submit']"));
        button.click();
    }

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

更新: 我试过了

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() 工作正常,click() - 不行。

【问题讨论】:

  • 我不确定它是如何使用的,但是从文档中单击不会等待下一页加载,而提交将等待操作完成。这可能是原因吗?
  • 你能用相关的 HTML 更新问题吗?
  • 我加到高2个div,请注意,够不够
  • @gvmani:听起来很合理,谢谢...让我们等待更多解释。
  • 按钮是否在表单中?如果是这样,请显示表单元素。

标签: java selenium selenium-webdriver junit


【解决方案1】:

submit()

submit() 方法定义为:

void submit()

Throws:
NoSuchElementException - If the given element is not within a form

根据JavaDocs,当您在WebElement 上调用submit() 时,如果此当前元素是表单或表单中的元素,那么它将提交给远程服务器。如果这导致当前页面发生变化,则此方法将阻塞,直到加载新页面。

click()

click() 方法定义为:

void click()

Throws:
StaleElementReferenceException - If the element no longer exists as initially define

根据JavaDocs,当您在WebElement 上调用click() 时,如果这会导致加载新页面,您应该放弃对该元素的所有引用以及执行的任何进一步操作在这个元素上会抛出一个 StaleElementReferenceException。请注意,如果 click() 是通过发送本机事件(这是大多数浏览器/平台上的默认事件)完成的,则该方法将等待下一页加载,调用者应验证自己.要单击的元素有一些 ExpectedConditions。该元素必须可见,并且必须有一个heightwidth大于0


您的用例:

在您的用例中,目标 WebElement //button[@type='submit']&lt;form&gt; 标记内。您可以通过 submit() 成功按下它,因为它会阻止该方法,直到新页面由于在前一页上提交而完全加载。因此以下工作:

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

但是当您尝试如下 click() 方法时,click() 作为一个原生事件不会等待新页面完全加载。因此调用click() 方法失败。

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

您的代码试用:

验证 button.isEnabled() 条件不会为我们获取所需的结果,如果元素可点击或不可点击,因为 isEnabled() 验证 Is the element currently enabled or not? This will generally return true for everything leaving out the disabled input elements。但如果WebElement 可见可交互可点击或不是。


解决办法:

根据提供的详细信息,调用 click() 方法的解决方案是诱导 WebDriverWaitExpectedConditions 子句设置为 elementToBeClickable 如下:

new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@type='submit']"))).click();

更新:

最佳实践:

根据更新后的HTML 和您随后提到的关于在您的应用中使用Angular4 的评论,更有效的方法是构造xpath 来模拟HTML DOM,如下所示:

new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//form[@class='search-form ng-touched ng-dirty ng-valid']//button[@class='btn btn__primary' and @type='submit']"))).click();

【讨论】:

  • 我已经尝试过了(请参阅有问题的“更新”部分),但它不起作用。一些 cmets 让我阅读了有关 Angular~Selenium 友谊的信息,所以我将继续讨论最近的日子。 (是的,Angular4 在我的应用中使用)。
  • 您有机会浏览我更新的答案吗?请告诉我状态。
  • 我的主要工作有点超负荷,将按工作顺序尝试,谢谢。
  • 不,仍然是相同的行为(测试“绿色”,但 click() 后没有操作)。我检查了页面上存在的 Xpath/元素。
【解决方案2】:

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

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

  1. 该按钮可见但未启用。
  2. 驱动程序正在寻找 2 searchButton 元素的实例。

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

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

【讨论】:

  • 1) e.size = 1. 2) 我尝试了建议的 click() - 但同样 - 在浏览器启动时未按下按钮。感谢您指点我检查可见性/启用性,我会使用它。
  • 您应该尝试启用元素而不是可见元素。另外,您能告诉我您使用的是哪种浏览器吗?
  • 我提到“启用”了 ofc,抱歉,打错了。浏览器 - 火狐 57.02
  • 你可以试试下面的代码: WebElement element = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.visibilityOfElementLocated(By .id("//button[@type='submit']" ))); element.sendKeys(Keys.RETURN); AND JavascriptExecutor 执行器 = (JavascriptExecutor) 驱动程序; executor.executeScript("arguments[0].click();", element);
  • 它有效,伙计)。但问题是关于直接 click() 命令。我可以只使用 submit(),它无需 JS 调用即可工作。让我玩例如已启用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多