1

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

2 Answers2

1

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

matebende
  • 543
  • 1
  • 7
  • 21
0

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)
frianH
  • 7,295
  • 6
  • 20
  • 45