Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium:submit()可以正常工作,但click()不行_Java_Selenium_Selenium Webdriver_Junit - Fatal编程技术网

Java Selenium:submit()可以正常工作,但click()不行

Java Selenium:submit()可以正常工作,但click()不行,java,selenium,selenium-webdriver,junit,Java,Selenium,Selenium Webdriver,Junit,我有一个提交按钮,这是页面上唯一的一个按钮,它是在表单中 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>

我有一个提交按钮,这是页面上唯一的一个按钮,它是在表单中

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>
我正在通过submit()成功按下它(让我跳过WebDriver init,很好):

(并执行一些搜索)

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

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

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

更新: 我试过了


但是仍然一样-submit()可以正常工作,click()-不行。

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

现在,为什么
searchButton.click()不在此处工作可能有以下原因

  • 按钮可见,但未启用
  • 司机正在找2号
    searchButton
    元素的实例
  • 建议:评估以下代码并检查它是否返回多个元素。如果是这样的话,那么你点击了错误的实例

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

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

    现在,为什么
    searchButton.click()不在此处工作可能有以下原因

  • 按钮可见,但未启用
  • 司机正在找2号
    searchButton
    元素的实例
  • 建议:评估以下代码并检查它是否返回多个元素。如果是这样的话,那么你点击了错误的实例

    List<WebElements> e = driver.findElements(By.xpath("//button[@type='submit']"));
    
    submit()
    方法定义为:

    void submit()
    
    Throws:
    NoSuchElementException - If the given element is not within a form
    
    void click()
    
    Throws:
    StaleElementReferenceException - If the element no longer exists as initially define
    
    根据在
    WebElement
    上调用
    提交()
    时的说明,如果此当前元素是表单或表单中的元素,则此元素将提交到远程服务器。如果这导致当前页面发生更改,则此方法将阻止,直到加载新页面

    单击()
    方法定义为:

    void submit()
    
    Throws:
    NoSuchElementException - If the given element is not within a form
    
    void click()
    
    Throws:
    StaleElementReferenceException - If the element no longer exists as initially define
    
    根据在
    WebElement
    上调用
    单击()
    ,如果这导致加载新页面,则应放弃对该元素的所有引用,并且对该元素执行的任何进一步操作都将抛出StaleElementReferenceException。请注意,如果click()是通过发送本机事件来完成的(这是大多数浏览器/平台上的默认设置),那么该方法将不会等待下一页加载,调用方应该自己验证。对于要单击的元素,存在一些预期条件。元素必须可见,并且其高度和宽度必须大于0


    您的用例: 在您的用例中,目标
    WebElement
    //按钮[@type='submit']
    位于
    标记内。您可以通过
    submit()
    成功按下它,因为它会阻止该方法,直到上一页提交后新页完全加载。因此,开展了以下工作:

    WebElement searchButton = driver.findElement(By.xpath("//button[@type='submit']"));
    searchButton.submit();
    
    但是当您尝试下面的方法时,
    click()
    作为本机事件不会等待新页面完全加载。因此调用
    click()
    方法失败

    WebElement button = driver.findElement(By.xpath("//button[@type='submit']"));
    button.click();
    
    您的代码试用: 验证
    按钮。isEnabled()
    如果元素可单击或不可单击,则条件不会获取所需结果,因为验证
    元素当前是否已启用?对于不包含禁用的输入元素的所有内容,这通常都将返回true
    。但是如果
    是可见的、可交互的和可点击的,那么
    不一定会被验证


    解决方案: 根据提供的详细信息,调用
    click()
    方法的解决方案是将
    预期条件
    子句设置为如下:

    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();
    
    submit()
    方法定义为:

    void submit()
    
    Throws:
    NoSuchElementException - If the given element is not within a form
    
    void click()
    
    Throws:
    StaleElementReferenceException - If the element no longer exists as initially define
    
    根据在
    WebElement
    上调用
    提交()
    时的说明,如果此当前元素是表单或表单中的元素,则此元素将提交到远程服务器。如果这导致当前页面发生更改,则此方法将阻止,直到加载新页面

    单击()
    方法定义为:

    void submit()
    
    Throws:
    NoSuchElementException - If the given element is not within a form
    
    void click()
    
    Throws:
    StaleElementReferenceException - If the element no longer exists as initially define
    
    根据在
    WebElement
    上调用
    单击()
    ,如果这导致加载新页面,则应放弃对该元素的所有引用,并且对该元素执行的任何进一步操作都将抛出StaleElementReferenceE