Search code examples
pythonseleniumautomationappium

(Selenium - Python) Finding one of two elements in webDriver


In my app, I can expect one of two elements (a or b) to appear. I currently have: el1 = driver.find_element_by_id("a")

But, I'd like to do something like: el1 = driver.find_element_by_id("a" or "b")

Is that possible in selenium? I'm doing this in Python, if that helps.


Solution

  • Simple and straightforward solution:

    try:
        # Look for first element
        el1 = driver.find_element_by_id('a')
    except selenium.common.exceptions.NoSuchElementException:
        # Look for the second if the first does not exist
        el1 = driver.find_element_by_id('b')