Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7175715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:21:36+00:00 2026-05-28T16:21:36+00:00

I am doing the following in selenium 2/webdrive using python and firefox… I am

  • 0

I am doing the following in selenium 2/webdrive using python and firefox…

I am opening some web pages that I need to check for a specific string – which, if present, means it is a good page to parse.

The phrase I am looking for is an h2 element similar to this:

<h2 class="page_title">Worlds Of Fantasy : Medieval House</h2>

If that h2 is missing, I know I don’t need to work on it, just return and get the next in line.

In the code I have a try/exception/else block that looks for the phrase, if it sees it it passes on to the next part of the function. If not, it should go to the else, which tells it to return.

There are 2 pages called in this test – the first has the phrase, the second does not.

The first page is opened, and passes the test.

The second page is opened, and I get an exception report – but it never returns to the calling code in main…it just stops.

Why isn’t the exception fallowing the proper path to return?

Here is the code:

    #!/usr/bin/env python

from selenium import webdriver
from selenium.webdriver import Firefox as Browser
from selenium.webdriver.support.ui import WebDriverWait


browser = webdriver.Firefox()

def call_productpage(productlink):
    global browser

    print 'in call_productpage(' + productlink + ')'
    browser.get(productlink)
    browser.implicitly_wait(8)

    #start block with <div class="page_content"> 
    product_block = browser.find_element_by_xpath("//div[@class='page_content']");

    # <h2 class="page_title">Worlds Of Fantasy : Medieval House</h2>
    try:
        product_name = product_block.find_element_by_xpath("//h2[@class='page_title']");
    except Exception, err:
        #print "Failed!\nError (%s): %s" % (err.__class__.__name__, err)
        print 'return to main()'
        return 0
    else:
        nameStr = str(product_name.text)
        print 'product_name:' + nameStr
    finally:
        print "test over!"
        return 1

test1 = call_productpage('https://www.daz3d.com/i/3d-models/-/desk-clocks?spmeta=ov&item=12657')
if test1:
    print '\ntest 1 went OK\n'
else:
    print '\ntest 1 did NOT go OK\n'

tes2 = call_productpage('https://www.daz3d.com/i/3d-models/-/dierdre-character-pack?spmeta=ov&item=397')
if test2:
    print '\ntest 2 went OK\n'
else:
    print '\ntest 2 did NOT go OK\n'

And here is a screenshot of the console showing the exception I get:

enter image description here

One other option I thought about using was to get the source of the page from the webdriver and do a find to see if the tag was there – but apparently there is no easy way to do THAT in webdriver!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:21:38+00:00Added an answer on May 28, 2026 at 4:21 pm

    If you don’t know which exception to expect, use empty except and traceback:

    import traceback
    
    try:
        int('string')
    except:
        traceback.print_exc()
        print "returning 0"
    
    # will print out an exception and execute everything in the 'except' clause:
    # Traceback (most recent call last):
    #   File "<stdin>", line 2, in <module>
    # ValueError: invalid literal for int() with base 10: 'string'
    # returning 0
    

    But from the stack trace you already do know the exact exception name, so use it instead:

    from selenium.webdriver.exceptions import NoSuchElementException
    
    try:
        #...
    except NoSuchElementException, err:
        #...
    

    UPDATE:

    You just get an exception before the try ... except, here:

    product_block = browser.find_element_by_xpath("//div[@class='page_content']");
    

    and not here:

    product_name = product_block.find_element_by_xpath("//h2[@class='page_title']");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help doing the following: a preprocessor macro label(x) shall output #x, e.g.,
I am using selenium for sometime and doing good with it. I would like
I'm doing the following code that construct a distance matrix between each point and
I want to share a link using Facebook Graph API. I am doing following.
I'm using the Selenium 2.0 web drivers to try and login to a page.
I am doing the following things to make sure that my application launches only
Instead of doing the following everytime start(); // some code here stop(); I would
Suppose I am doing the following: using (OracleConnection conn = new OracleConnection(connStr)) { OracleTransaction
Im doing webtest using selenium Webdriver in C#. But I'm having a problem where
I'm currently doing the following to use typed datasets in vs2008: Right click on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.