Fwd: Re: How to improve this code?

Duncan Booth duncan.booth at invalid.invalid
Wed Sep 16 08:13:22 EDT 2009


Hendrik van Rooyen <hendrik at microcorp.co.za> wrote:

>   def are_elements_present(eleLocators):
>     elePresent=False
>     if not eleLocators:
>         return False
> 
>     for ele in eleLocators:
>         if selenium.is_element_present(ele):
>             elePresent=True
>         else:
>             elePresent=False
>             print 'cannot find this element= '+str(ele)
>             break
>     return elePresent
> 
> 
> 
> Now suppose page HTML contains with these IDs ( ID is an attribute
> like <input id="inp1" />) = div1,div2,div3,div4,div5,inp1,inp2
> and if I call the above method this way are_elements_present
> ([div1,div2,inp1,inp2]) then it should return True. If I call like
> are_elements_present([div1,div2,div10,inp1]) it should return False.
> So I hope I've explained myself. Now all I'm looking for is to write
> are_elements_presents() in a more Pythonic way. So please let me know
> if I can write are_elements_present() in more smart/shorter way.
> 
The obvious way to write this would seem to me to be (untested code):

  def are_elements_present(eleLocators):
     return all(selenium.is_element_present(ele) for ele in eleLocators)

However be aware that inverts the result for the case where eleLocators is 
an empty list which may or may not matter in this situation.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list