I am doing the following in selenium 2 / webdrive using python and firefox ...
I open some web pages that I need to check for a specific line, which, if present, means that it is a good page to parse.
The phrase I'm looking for is an h2 element like this:
<h2 class="page_title">Worlds Of Fantasy : Medieval House</h2>
If this h2 is missing, I know that I do not need to work on it, just go back and get the next line.
In the code, I have a try / exception / else block that searches for a phrase if it sees that it goes to the next part of the function. If not, he should go to else, which tells him to return.
In this test, 2 pages are called - the first has the phrase, the second does not.
The first page opens and passes the test.
The second page is open, and I get an exception report, but it never returns to the calling code in main ... it just stops.
Why not an exception that returns the correct path?
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 that I get:

Another option that I was thinking about using was to get the page source from webdriver and find find to see if there is a tag there, but apparently there is no easy way to do this in webdriver!