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.
you can use xpath in that case :
el1 = driver.find_element_by_xpath("//*[@id= 'a' or @id = 'b']")
I am assuming that two tags in HTML
has two different ids
, you can write or
condition in xpath like above.
Update :
Heads up to anyone implementing this with an android app, the syntax is:
el1 = driver.find_element_by_xpath("/hierarchy/a" or "/hierarchy/b")
在纯 Python 中,您可以这样做:
el1 = driver.find_element_by_id("a") if driver.find_element_by_id("a") else driver.find_element_by_id("b") if driver.find_element_by_id("b") else None
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.