james mooney
james mooney

Reputation: 55

How can I check for a form error in html?(Python, Selenium)

Hello so I am trying to login and then scan the html for a form error. I am using the selenium library. heres the html code I am trying to find:

<div class="_3_2jD" id="form_error"><p>Please check the code we sent you and try again.</p></div>

this is my code I am using to try and find it:

       if "form_error" in self.chrome_browser.page_source:
                print("trying again" + random_code)

any suggestions :D

Upvotes: 1

Views: 232

Answers (2)

frianH
frianH

Reputation: 7563

You can check the element len, utilize .find_elements_*:

if(len(self.chrome_browser.find_elements_by_id('form_error'))>0):
    print("trying again" + random_code)

Upvotes: 0

matebende
matebende

Reputation: 609

The easiest way is to use find_element_by_id() or find_element_by_xpath()

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.your_page.com")
driver.maximize_window()
time.sleep(5)
try:
    inputElement = driver.find_element_by_id("form_error")  #or  driver.find_element_by_xpath("//div[contains(@id,'form_error')]")
except NoSuchElementException:
    return False

https://stackoverflow.com/a/9587938/12451425 https://www.techbeamers.com/locate-elements-selenium-python/#locate-element-by-id

Upvotes: 1

Related Questions