1

I have 10 links in a list, upon clicked,which will open a new window. Different links would yield different set of pages, however i have 3 common elements for all 10 links.

Following is the function example.

def handle_window(self):
    self.driver.go_to_new_window()

    try: # block 1
        elements = ["element1", "element2", "element3"]
        for element in elements:

            try:  #block 2
                self.assertEqual(True, is_exist_in_new_window(element)))
            except:
                continue

    except:
        # in 'try block 2' if assert yields true at least once,
        print 'passed'
        # if it fails for all 3 elements,
        print 'failed'
    self.driver.close_current_window()
    self.driver.go_to_main_window()

I am not sure how do i evaluate the results of 'try block 2', so that to do some action in block 1. Any possible solutions ?

2 Answers 2

2

If "element1", etc. are meant to be CSS selectors, the most efficient way would be:

elements = ["element1", "element2", "element3"]
self.assertTrue(exists_in_new_window(",".join(elements)))

(I've renamed is_exist_in_new_window to exists_in_new_window.) The , operator in CSS means "or". So the CSS selector passed to exists_in_new_window means you are looking for "element1" or "element2" or "element3". Doing it this way will need one round-trip between the Selenium client and the browser, no matter what. Note that the code above is not meant to handle meaningfully the case where elements is a zero-length list.

With XPath selectors you could use the | operator to perform a similar transformation. In this case, I would want to additionally use parentheses to preserve semantics of the individual selectors so something like "(" + ")|(".join(elements) + ")". (I believe the semantics issue does not arise in CSS due to CSS' very rigid syntax.)

In the more general case where it is not possible to combine the search criteria into one expression, one can fall back onto alecxe's suggestion:

elements = ["element1", "element2", "element3"]
self.assertTrue(any((exists_in_new_window(element) for element in elements)))

This method causes a minimum of min(1, len(elements)) round-trips between the Selenium client and the browser and a maximum of len(elements) depending on what is present on the page.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use any() to check if at least one element existed on a page:

elements = ["element1", "element2", "element3"]
value = any((is_exist_in_new_window(element) for element in elements))
self.assertTrue(value)

This code assumes is_exist_in_new_window() returns True or False.

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.