O
O
Owl_Marta2019-10-29 17:26:57
Python
Owl_Marta, 2019-10-29 17:26:57

Check text on modal window and close it (Selenium Python)?

I ask for help in solving the problem. Task: open a popup modal window from a given page, make sure that it contains the desired text, close the modal window. The test passes, the text matches, but not the window closes, but the entire browser. Modal is not a frame.
For the task, only the modal window should close, the browser should remain open.
Available code

trial = browser.find_element_by_xpath("//a[@href='/prices#trial']").click() #переход на заданную страницу
    time.sleep(5)
    button_trial = browser.find_element_by_class_name("btnBuy.trial.notRegisterStatus").click() #кнопка включающая модальное окно
    
    time.sleep(5)

    welcome_text_login = browser.find_element_by_class_name("titleWrapper")
    welcome_text = welcome_text_login.text
    return welcome_text

    assert "Login" == welcome_text

    time.sleep(5)

    modal_login = browser.find_element_by_xpath("//div[@class='modalWrapper']/div[@class='titleWrapper']/div[@class='btnWrapper']/button[@class='hideModal']").click()

Thanks in advance :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Byxo Cyze, 2019-10-31
@byxo-cyze

I hope this is a popup:

<!DOCTYPE html>
<html>

  <head><title>Popup</title></head>

  <body>
    <div> Hello, World! </div>

    <button id="myBtn">Открыть окно</button>


<div id="myModal" class="modal">


  <div class="modal-content">
    <div class="modal-header">
      <span class="close">&times;</span>
      <h2>Шапка модального окна</h2>
    </div>
    <div class="modal-body">
      <p>Какой-то текст в теле модального окна</p>
      <p>Ещё другой текст...</p>
    </div>
    <div class="modal-footer">
      <h3>Футер модального окна</h3>
    </div>
  </div>

</div>


  </body>	

<style>
body {font-family: Arial, Helvetica, sans-serif;}

#myBtn {
  background-color: #9C27B0;
  color: white;
  padding: 12px 16px;
  font-size: 18px;
  font-weight: bold;
  border: none;
  cursor: pointer;
  width: 180px;
}


.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}


.modal-content {
    position: relative;
    background-color: #E1BEE7;
    margin: auto;
    padding: 0;
    border: 5px solid #7B1FA2;
    width: 50%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}


@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0} 
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}


.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding: 2px 16px;
    background-color: #9C27B0;
    color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
    padding: 2px 16px;
    background-color: #9C27B0;
    color: white;
}
</style>
  <script>

    var modal = document.getElementById('myModal');


var btn = document.getElementById("myBtn");


var span = document.getElementsByClassName("close")[0];


btn.onclick = function() {
    modal.style.display = "block";
}


span.onclick = function() {
    modal.style.display = "none";
}


window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
  </script>

</html>

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get("http://localhost:5000")


driver.find_element_by_id('myBtn').click()

time.sleep(1)

text = driver.find_element_by_class_name('modal-body').text
assert 'Какой-то текст' in text

driver.find_element_by_class_name('close').click()

input()
driver.close()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question