pythonselenium-webdriverpopupautotest

The text of a pop-up window is not displayed in the html-markup, how can I check it using python + selenium?


I’m designing an autotest using selenium + python and facing the described problem.

When you click on the "Register" button, a pop-up window with a text appears. I need to check whether the text in this pop-up window is correct. The problem is this window is not part of the html layout, so I can't access it with selenium. Please, can you suggest how to solve this problem?Pop-up window

The script is below:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from supporting_scripts import generate_random_string
from supporting_scripts import password
from configuration import chrome_options
import time


driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)


class Registration:

    driver.execute_script("window.open('https://site.example.com/service/welcome', '_blank');")

    window_handles = driver.window_handles

    driver.switch_to.window(window_handles[-1])

    Button_Registration = WebDriverWait(driver, 60).until(
        ec.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[3]/div/div[2]/p[1]")))
    Button_Registration.click()
    print('Registration click success ✅ ')

    Button_Registration_Check = WebDriverWait(driver, 60).until(
        ec.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div/form/div/button")))
    Button_Registration_Check.click()

    time.sleep(10)

I tried using different libraries and none of them worked for me.

The thing I know for sure is that this pop-up is a browser window, which is shown due to the input rule - type="email"


Solution

  • That popup you are seeing is default HTML behavior for an INPUT field in a FORM that is marked required. For a simple example,

    <!DOCTYPE html>
    <html>
    <body>
    
    <form>
        <input required id="username">
        <button type="submit">Submit</button>
    </form>
    
    </body>
    </html>
    

    When you click the Submit button, it shows the same message. So, you don't need to validate it. What you might validate instead is if the required attribute is on the different fields. That way you are confirming that the specific field is required and will display the default message if the field is empty when the form is submitted.


    I rewrote your code to simplify it and your locators, added a wait, and added an assert for the required attribute, if you are interested.

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    
    URL = "https://it.videomost.com/service/welcome?lang=en"
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get(URL)
    
    wait = WebDriverWait(driver, 10)
    wait.until(EC.element_to_be_clickable((By.XPATH, "//p[text()='Sign up']"))).click()
    email_id_required = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[name='email']"))).get_attribute("required")
    
    assert email_id_required == "true", "Verify Email ID field is required"
    
    driver.quit()