Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

java - Selenium: Button in modal content is not clickable

I want to Click the "OK" button during a Selenium test but the Element is not visible.

 driver.findElement(By.xpath("//*[@id="5f6e7b16-0fa1-4db6-869b-3a6ba6b0fafe"]")).click();
<div class="bootstrap-dialog-footer-buttons">
    <button class="btn btn-default" id="5a4bb849-7a61-4603-9ef2-f9e0ecab4523">
        <span class="glyphicon glyphicon-ban-circle"></span> Cancel
    </button>
    <button class="btn btn-warning" id="f7f4b18b-2ba2-4c1e-b541-a254c080f398">
        <span class="glyphicon glyphicon-ok"></span> Ok
    </button>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think in your DOM, the button id is changing dynamically. Whenever page reload it will generating new id. There is different button id you used in your Selenium code and HTML. So, I suggest you go with className. Try below code and hope it works for you.

        //If the Element is not visible then wait until that element is not visible
        new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.className("btn btn-warning")));

        //If the element is visible but not Clickable then wait until that element get Clickable.       
        new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.className("btn btn-warning")));

        //Then simply click the button
        driver.findElement(By.className("btn btn-warning")).click();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...