0

I am trying to verify a validation, can be seen here https://groups.google.com/forum/#!topic/webdriver/sUy6IKpQsHw, this link is not mine but I want to verify this text Please fill out this field., or any other better idea ? how to verify it

What I did is as below:

msg = self.driver.find_element_by_id("username")
print(msg.get_attribute("value"))
time.sleep(10)
assert msg.get_attribute("value") == "Please fill out this field."

Print has given following output > print(msg.get_attribute("value")) and assert has given AssertionError, I have been through many answers non helped me

HTML:

<input name= "username" id="username" required>

I tried [print(msg.get_attribute("value").values) to see whats there and it has given following error AttributeError: 'unicode' object has no attribute 'values'

JeffC
  • 22,180
  • 5
  • 32
  • 55
Maria
  • 11
  • 4
  • @DebanjanB non of that is python solution, any idea of python solution ? – Maria Dec 15 '19 at 18:35
  • @DebanjanB yes I found the solution but it gives following error username = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.cell.small-21.form-text.required#edit-name[name='username']"))) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = method = message = '' – Maria Dec 15 '19 at 18:49
  • @DebanjanB actually I have your line of code , in my case name of field was username, so I replaced and tried, I am sorry I dun have any idea of how to do it – Maria Dec 15 '19 at 18:52

3 Answers3

0

To verify the presence of the validation message with text as Please fill out this field. you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#username[name='username']"))).get_attribute("validationMessage"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "input[@id='username' and @name='username']"))).get_attribute("validationMessage"))
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can get validationMessage attribute to get validation message:

msg = self.driver.find_element_by_id("username")
validation_message = msg.getAttribute("validationMessage")
# or
# validation_message = self.driver.execute_script("return arguments[0].validity.valid;", msg)

assert validation_message == "Please fill out this field."
Sers
  • 12,047
  • 2
  • 12
  • 31
0

FOllwoing should work

username = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.ID, "username")))
assert username.get_attribute("validationMessage") == "Please fill out this field."
Sara
  • 181
  • 1
  • 3
  • 16