Hello everyone - I just ran into another issue related to CSS selector especially ‘.has-error’ I still cant find a solution. Anyone here ever dealt with this issue before? pls kindly help. I am stuck at this point below:
‘’‘selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .has-error’‘’
Related code between my template/selenium:
Python part:
"You can't have an empty list item"))```
html part:
<div class="form-group has-error">
<span class="help-block">{{ error }}</span>
</div>
{% endif %}
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Never used it but most likely the selector returns a list and thus does not have a text field.
.has-error element is actually present in the HTML when Selenium is trying to locate it. This class may only appear conditionally (e.g., if there’s an error in the form), so the element may not always be there when the page first loads.Try adding an explicit wait to ensure the element is fully loaded before Selenium attempts to locate it:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Wait for the element with the .has-error class to appear
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".has-error"))
)
.has-error appears dynamically (e.g., after form submission), ensure the error message is triggered properly before Selenium looks for the element. You can add an assertion or log step to confirm the flow is working as expected.Ensure the CSS selector you’re using is correct. The correct syntax is:
driver.find_element_by_css_selector(".has-error")
If there are multiple .has-error elements on the page, you may want to use find_elements_by_css_selector and loop through them.
driver.switch_to.frame("frame_name_or_id")
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.