samedi 3 octobre 2015

Selenium/python 2.7 How to use if/else with find_element?

What I want is to say if element1 is displayed, click, if not check if element2 is displayed, click, etc. But my code stops when an element isnt there. If element1 is there, it clicks it, if it is not there, the code stops but I want it to keep going through each elif until it finds one, any help is appreciated.

from selenium import webdriver

fp = webdriver.FirefoxProfile('')
driver = webdriver.Firefox(firefox_profile=fp)
driver.set_window_size(1400, 1000)
driver.get('')

elem1 = driver.find_element_by_xpath("//img[@title='']")

elem = driver.find_element_by_xpath("//img[@title='']")

if elem.is_displayed():
    elem.click()
    print "true"
elif elem1.is_displayed():
    elem1.click()
    print "1true"
else:
    print "false"

Aucun commentaire:

Enregistrer un commentaire