2

On my web page are two different types of errors.

The first one is:

<div class="warning" data-text="text-field-error" dir="ltr">Diese ID ist nicht verfügbar.</div>

And the second one is:

<div class="separator-notice text-notice text-margin theme-noticeerror-font" dir="ltr">Bei der Verbindung zum Server ist eine Zeitüberschreitung aufgetreten.</div>

For example, if I try the wrong email, it doesn't have a specific value how often you can repeat. From time to time it's different. Now the first error means that this email (in my case ID) isn't valid.

The second error's meaning is that the Server connection has failed. Now with Selenium I want to handle these two different errors something like this:

for line in lines:
    driver.find_element_by_id(input_id).send_keys(line)
    driver.find_element_by_class_name(check_available).click()
    count += 1
    time.sleep(1)
    try:
        # Check if I get error one
    except:
        # I got an error two
    else:
       pass

I've already looked around at stack but couldn't find anything that matches my requirements. I also have tried it with xpath by text like this:

try:
    driver.find_element_by_xpath("//div[contains(text(), ' Diese ID ist nicht verfügbar.')]")
except:
      # It has to be error two

So my question is: How can I check which error I currently have and how can I work with that error. For example

if error1:
   print("error_one")
if error2:
   print("error_two")
JeffC
  • 22,180
  • 5
  • 32
  • 55

2 Answers2

1

You can do that even without try - except at all.
You can do something like this:

for line in lines:
    driver.find_element_by_id(input_id).send_keys(line)
    driver.find_element_by_class_name(check_available).click()
    count += 1
    time.sleep(1)
    first = driver.find_elements_by_xpath("//div[contains(text(), 'Diese ID ist nicht verfügbar.')]")
    second = driver.find_elements_by_xpath("//div[contains(text(), 'Bei der Verbindung zum Server ist eine Zeitüberschreitung aufgetreten')]")
    if first:
        #you have got the first notification
    if second:
        #you have got the second notification

You can also do this with the expected conditions and with try - except, but this approach looks to be the simplest.

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • 1
    Even though OP used a sleep, you should follow best practices and use a proper wait but this is the best approach after that fix. – JeffC Jan 13 '22 at 03:38
  • I totally agree with you @JeffC, I just followed the existing code OP used here to make it working. – Prophet Jan 13 '22 at 11:23
  • 1
    You should use expected conditions explicit waits or setting implicit wait rather than putting hardcoded delays. In this case I think it would be preferable to set the `implicitly_wait`, but we need to see the entire project to decide what will fit better – Prophet Jan 13 '22 at 11:43
  • 1
    I will try to change my code the way you are talking about. Thank yaal for helping and also for critism to my code I really appreciate it ;) @Prophet if u want to i can show u my code on dc: rsx#9282 –  Jan 27 '22 at 14:23
0

To probe both the errors you need to wrap up the validation within a try-except{} block inducing WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategies:

for line in lines:
    driver.find_element_by_id(input_id).send_keys(line)
    driver.find_element_by_class_name(check_available).click()
    count += 1
    time.sleep(1)
    try:
        WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(., 'Diese ID ist nicht verfügbar.')]")))
    except TimeoutException:
        WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(,, 'Bei der Verbindung zum Server ist eine Zeitüberschreitung aufgetreten.')]")))

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
from selenium.common.exceptions import TimeoutException
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 2
    Sleeps are a bad practice, which you know, and isn't necessary here. You've also instantiated two instances of `WebDriverWait()`. Instead declare a single `wait = WebDriverWait(driver, 20)` and reuse it. The way you've written this, if the first wait throws you've waited 20s for nothing. If both fail, you've waited 40s for nothing. Instead you should use a proper wait for something on the page you expect to exist and then check for both error messages with no wait. Now you've covered all the cases with no extra time wasted. See Prophets answer for the better approach. – JeffC Jan 13 '22 at 03:42