Selenium WebDriver Python – can’t capture the error message that shows up
In simple terms, we can create XPath expressions only for elements in the HTML structure. When a tooltip acts as a validation message because of the type="email" attribute, it’s tricky to directly inspect or make an XPath for it. However, if you want to check the validation message, you can easily get the message content using the get_attribute() method.
Here’s the code I used to tackle this task.
import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.uke.gov.pl/formularz-kontaktowy/")
driver.implicitly_wait(5)
driver.find_element(By.XPATH,"/html/body/div[1]/div[1]/div[2]/span[1]/a").click()
dropdown_element = driver.find_element(By.XPATH,"//select[@id='temat']")
dropdown = Select(dropdown_element)
dropdown.select_by_visible_text("Sekretariat Prezesa")
email = driver.find_element(By.XPATH,"//input[@id='email']")
email.send_keys("Michaeljames@_gmail.com")
email.send_keys(Keys.ENTER)
time.sleep(5)
message = driver.find_element(By.XPATH,"//input[@id='email']").get_attribute("validationMessage")
print(message)
You can additionally verify the presence of the validation message by executing $0.validationMessage in the console.
Read more here: Source link
