【问题标题】:Selenium - cannot click on an element inside a modalSelenium - 无法单击模式内的元素
【发布时间】:2018-06-05 06:36:28
【问题描述】:

我正在使用 Selenium 和 java,我无法点击模态框内的元素。 场景是这样的: 单击框架内的项目后,它会打开一个模态,我需要单击此模态内的一个元素,但我无法获取它。

我已经尝试过:

js.executeScript("document.getElementById('saveexit').scrollIntoView(true);");

我也尝试过这样的 switchTo():

while (itr.hasNext()) {
    String popup =  itr.next();
    System.out.println("itr: " + popup);
    driver.switchTo().window(popup);
}

这是我的模态的html:

<div class="modal-dialog">

    <div class="modal-content modal-custom-content">
        <div class="modal-header">
            ...
        </div>
        <div class="modal-body">
            <form id="formTo" class="form-container">
                <div class="row">
                    ...
                </div>
                <div class="small-space"></div>
                <input ...>
                <div class="row">
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                        ...
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                        ...
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                        ...
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                        ...
                    </div>
                </div>
                <div class="small-space"></div>
                <div class="row"> 
                    ...
                </div> 
            </form> 
        </div>
        <div class="small-space"></div>
        <div class="modal-footer">
            <div class="row text-center"> 
                <div class="col-md-6 col-sm-6 col-xs-12">
                    <button class="btn modal-button full-btn" id="saveexit" type="button">SAVE AND EXIT</button>
                </div>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    ...
                </div>
            </div>
        </div>
    </div>
</div>

这是从 firefox 开发工具中获取的 CSS 路径:

html.no-touch body div.remodal-wrapper.remodal-is-opened div.modaliAdesione.remodal.remodal-is-initialized.remodal-is‌​-opened div.modal-dialog div.modal-content.modal-custom-content div.modal-footer div.row.text-center div.col-md-6.col-sm-6.col-xs-12 button#saveexit.btn.modal-button.full-btn 

永远找不到对象。

  • 问题 1:如果元素在模态框内,则必须对其进行管理 不同?
  • 问题2:如何最终点击按钮 saveexit 工作正常吗?

这里分享一个html的sn-p代码:https://codeshare.io/arLW9q

这里是java代码:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"saveexit\"]")))

我也尝试过:

cssSelector: #saveexit
cssPath: html.no-touch body div.remodal-wrapper.remodal-is-opened div.modaliAdesione.remodal.remodal-is-initialized.remodal-is-opened 
div.modal-dialog div.modal-content.modal-custom-content div.modal-footer div.row.text-center div.col-md-6.col-sm-6.col-xs-12 
button#saveexit.btn.modal-button.full-btn
xpath: //*[@id="saveexit"]

请注意:如果我从浏览器的控制台运行document.getElementById('saveexit').click();,它会成功

【问题讨论】:

  • 模式不是可以切换到的窗口。它只是页面上的 HTML。您尝试过哪些 Java 代码,结果如何?错误信息等
  • 我尝试的Java代码是js.executeScript...结果只是一个错误,说saveexit不存在

标签: javascript java selenium


【解决方案1】:

当您按照最佳实践使用 Selenium-Java 客户端时,首要的试验必须是调用高效且经过验证的 click() 方法。根据 click() 方法导致的错误,我们可以使用其他替代解决方案。

从您对JavascriptExecutor 和switchTo().window() 的代码试验中可以看出,您没有很好地识别出代表SAVE AND EXIT 按钮的WebElement。

要点击 SAVE AND EXIT 按钮,您可以使用以下代码块:

new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='modal-dialog']//div[@class='modal-footer']//button[@class='btn modal-button full-btn' and @id='saveexit']"))).click();

【讨论】:

  • 谢谢 Deb,我会尽快尝试并接受它是否有效
  • 它不起作用,出现此“预期条件失败:等待 By.xpath 定位的元素的可见性:...”
  • 向我们展示更多outerHTML。可能的元素在 iframe 内或 xpath 不是唯一的。
  • 这是从 firefox 开发工具中获取的 CSS 路径:html.no-touch body div.remodal-wrapper.remodal-is-opened div.modaliAdesione.remodal.remodal-is-initialized.remodal -is-opened div.modal-dialog div.modal-content.modal-custom-content div.modal-footer div.row.text-center div.col-md-6.col-sm-6.col-xs- 12 按钮#saveexit.btn.modal-button.full-btn
  • 用 SO 志愿者帮助您的信息更新主要问题。
【解决方案2】:

我在脚本中使用 jquery 修复了它;

这是线路代码:

js.executeScript("$('#saveexit').trigger('click');");

我希望它可以帮助将来的人。

我不知道为什么普通的 javascript 不起作用...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2021-01-20
    • 2017-07-25
    相关资源
    最近更新 更多