I am trying to test a web portal using Selenium and Python. The problem is, I am unable to get around an error, in other words, I am unable to catch the alert.


Search for a user, if the user is not found, the website throws an error. I have a list that contains valid and invalid users to perform the search upon. When the search is successful, there are a few links I need to click to get to the user. If the user is not found, the links won't be displayed and an error is displayed instead. The problem is when I search for an invalid user, the code is looking for the links that are displayed when the a valid user is displayed.


from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.alert import Alert
from selenium.common.exceptions import NoAlertPresentException
import time

browser = webdriver.Chrome()  # type: WebDriver is Chrome

if option == 'number':
            nums = ['111', '123', '456']
            searchbox = browser.find_element_by_id("searchPattern")
            noUser = browser.find_element_by_xpath('//*[@id="includeError"]/div')
                for num in nums:
                    user_links = ['photos', 'message', 'mails']
                    for url in user_links:
                        if url in user_links:
                        elif url not in user_links:
                            print "No users found"
            except noUser:
                print "no users"
  • The alert is not a pop up but just a text alert on the page " User not found " – Dev Bin Aug 10 '18 at 21:08
  • If a user is found, the user details are displayed with the links for that user Photos Message mails – Dev Bin Aug 10 '18 at 21:09
  • please post console error – sameer joshi Aug 14 '18 at 10:33
  • I was able to fix the code by looking up the error text in the html source. – Dev Bin Aug 16 '18 at 18:38

The below code resolved my issue:

error = " No users found"

if error in page_source:
    print " error to console"
    // my code user defined action

The above code ignores the error if the error was there and continued with the search function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.