Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 22, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 22, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 22, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 22, 2025

      Sam Altman says ChatGPT’s viral Ghibli effect “forced OpenAI to do a lot of unnatural things”

      May 22, 2025

      How to get started with Microsoft Copilot on Windows 11

      May 22, 2025

      Microsoft blocks employees from sending emails that mention “Palestine” or “Gaza”

      May 22, 2025

      I missed out on the Clair Obscur: Expedition 33 Collector’s Edition but thankfully, the developers are launching something special

      May 22, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Perficient is Shaping the Future of Salesforce Innovation

      May 22, 2025
      Recent

      Perficient is Shaping the Future of Salesforce Innovation

      May 22, 2025

      Opal – Optimizely’s AI-Powered Marketing Assistant

      May 22, 2025

      Content Compliance Without the Chaos: How Optimizely CMP Empowers Financial Services Marketers

      May 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Sam Altman says ChatGPT’s viral Ghibli effect “forced OpenAI to do a lot of unnatural things”

      May 22, 2025
      Recent

      Sam Altman says ChatGPT’s viral Ghibli effect “forced OpenAI to do a lot of unnatural things”

      May 22, 2025

      How to get started with Microsoft Copilot on Windows 11

      May 22, 2025

      Microsoft blocks employees from sending emails that mention “Palestine” or “Gaza”

      May 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Unable to capture the alert in Selenium using Python

    Unable to capture the alert in Selenium using Python

    November 13, 2024

    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.

    Scenario:

    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.

    Code:

    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
    browser.get("https:URL")
    
    
    
    if option == 'number':
                nums = ['111', '123', '456']
                searchbox = browser.find_element_by_id("searchPattern")
                noUser = browser.find_element_by_xpath('//*[@id="includeError"]/div')
                try:
                    for num in nums:
                        searchbox.send_keys(num)
                        browser.find_element_by_xpath('//[@id="User"]/input[2]').click()
                        time.sleep(4)
                        user_links = ['photos', 'message', 'mails']
                        for url in user_links:
                            if url in user_links:
                                browser.find_element_by_link_text(url).click()
                                time.sleep(4)
                            elif url not in user_links:
                                print "No users found"
                        browser.switch_to_window(browser.window_handles[0])
                        time.sleep(5)
                        browser.find_element_by_id('searchPattern').clear()
                except noUser:
                    print "no users"
    

    Source: Read More

    Hostinger
    Facebook Twitter Reddit Email Copy Link
    Previous ArticleLoad testing on CI/CD Teamcity with Jmeter
    Next Article Problems to execute a UFT script by a VBScript file

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 23, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-48695 – CyberDAVA Privilege Escalation Vulnerability

    May 23, 2025
    Leave A Reply Cancel Reply

    Hostinger

    Continue Reading

    Perficient Insights: Dreamforce 2024 with Kristen Quick

    Development

    CISA Warns of Sitecore RCE Flaws; Active Exploits Hit Next.js and DrayTek Devices

    Development

    Multi-Task Learning with Regression and Classification Tasks: MTLComb

    Development

    12 Best Practices for React Developers

    Development
    GetResponse

    Highlights

    Development

    The Reserve Bank of India Issues Banking Advisory to Combat Rising Cybersecurity Threats

    July 1, 2024

    In a recent advisory, the Reserve Bank of India (RBI) has cautioned scheduled commercial banks…

    T-Shaped vs. V-Shaped Designers

    June 19, 2024

    Microsoft tells Windows 10 users to buy Copilot+ AI Windows 11 PC because it’s better

    April 27, 2025

    GRAF: A Machine Learning Framework that Convert Multiplex Heterogeneous Networks to Homogeneous Networks to Make Them more Suitable for Graph Representation Learning

    November 26, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.