Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...