Selenium - 无法单击模态内的元素 [英] Selenium - cannot click on an element inside a modal

查看:130
本文介绍了Selenium - 无法单击模态内的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Selenium and java and I cannot click on an element inside a modal. The scenario is this: after clicking on an item inside a frame, it opens up a modal and I need to click on an element inside this modal but I cannot get it.

我已经尝试过:

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

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

I also tried with switchTo() this way:

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

这是我的模态的html:

Here is the html of my modal:

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

this is the CSS Path as taken from firefox dev tool:

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 工作?

  • Question 1: if an element is inside a modal has to be managed differently?
  • Question 2: How to finally have the click on the button saveexit working?

这里共享了html的代码片段: https://codeshare.io/ arLW9q

here is shared a code snippet of the html: 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('sa veexit')。点击(); 从浏览器的控制台运行

Please note: if I run document.getElementById('saveexit').click(); from browser's console it works out

推荐答案

我用它修复了它我的脚本中的jquery;

I fixed it using jquery inside my script;

这里是行代码:

js.executeScript( $('#saveexit')。触发器('click'););

我希望将来可以帮助某人。

I hope it can help someone in future.

我不知道为什么普通的javascript无法运行...

I dont know why plain javascript was not working...

这篇关于Selenium - 无法单击模态内的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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