Alexey Brilenkov
Alexey Brilenkov

Reputation: 1

Python+Selenium: Check auto closing notification that does not belong to page source

There is a select tag with required option (required="required):

<select name="doc[]" id="doc" multiple="multiple" size="18" required="required">
  <option value="1">А.2 Проверочный перечень для ФИ плана сертификации ПО</option>
  <option value="2">А.3 Проверочный перечень для ФИ плана разработки ПО</option>
  ...

The purpose of this option is to disallow the pushing of commit button(s) if nothing is selected in a multiselect select tag. It looks like:

chrome notification

I need to check if some alert/notification/popup is displayed using Selenium + Python. The alert is not a standard window that could be seen in HTML and/or switch to, i.e. nothing happens with source HTML, the element cannot be inspected, it's displayed only for 5 seconds and then disappears.

It looks the same in different browsers (first picture, above, is from Chrome, same from Opera), this is from Firefox:

firefox

And IE: IE

MS Edge does not display any messages.

Also monitored using Chrome/Firefox devtools - nothing happens in the console, it's also not an animation (chrome F12 - dots menu - more tools - animations).

So how can I check for the presence of such kind of notifications?

Upvotes: 0

Views: 145

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193108

The popup which you are referring is the outcome of Constraint API's element.setCustomValidity() method.

Note: HTML5 Constraint validation doesn't remove the need for validation on the server side. Even though far fewer invalid form requests are to be expected, invalid ones can still be sent by non-compliant browsers (for instance, browsers without HTML5 and without JavaScript) or by bad guys trying to trick your web application. Therefore, like with HTML4, you need to also validate input constraints on the server side, in a way that is consistent with what is done on the client side.


Solution

To retrieve the text which results out from the element.setCustomValidity() method, you have to induce WebDriverWait for the element_to_be_clickable() and can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#doc[name^='doc']"))).get_attribute("validationMessage"))
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='doc' and starts-with(@name, 'doc')]"))).click()
    
  • Note : You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Upvotes: 0

Sers
Sers

Reputation: 12255

This is form validation:

element = driver.find_element_by_id('doc')

# boolean
is_valid = drive.execute_script('return arguments[0].validity.valid', element)

# get validation message
validation_message = element.get_attribute('validationMessage')

Upvotes: 1

Related Questions