jitlp
jitlp

Reputation: 47

How to check if an element exists using if - elif - else

I have a dropdown menu and I need to check its entries. If there is no entry , I can add new entries, if there is only one entry I remove it and add new entries and when I have many entries (>=2) I cannot proceed to adding entries. I can check it via person_rem_btn. If I have only one button doc_person_table:0:person_rem_btn I can proceed If I have a second button doc_person_table:1:person_rem_btn I cannot proceed.

I get this exception:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='frmMain:doc_person_table:1:person_rem_btn']/span[1]"}

However, that's the point, this element may not be available, I just check its existence. I would appreciate any help. Here is my code:

if driver.find_element_by_xpath("//*[@id='frmMain:doc_person_table:1:person_rem_btn']/span[1]") == True:
   print ("there are already many entries")
   driver.close()
elif (driver.find_element_by_xpath("//*[@id='frmMain:doc_person_table:1:person_rem_btn']/span[1]") == False and driver.find_element_by_xpath("//*[@id='frmMain:doc_person_table:0:person_rem_btn']/span[1]") == True):
   print ("there is only one entry, it will be removed to proceed")
   WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='frmMain:doc_person_table:0:person_rem_btn']/ span[1]"))).click()
else:
    print ("there is no entry, you can proceed")

Upvotes: 3

Views: 661

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193108

There are several approaches you can adapt and one of the approach would be to create a List of the dropdown entries inducing WebDriverWait for the visibility_of_all_elements_located() and probe the list size:

  • Using CSS_SELECTOR:

    list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "[id*='doc_person_table'][id$='person_rem_btn']>span")))
    if not list:
        print ("there is no entry, you can proceed")
        # other steps
    elif len(list) == 1
        print ("there is only one entry, it will be removed to proceed")
        # other steps
    else:
        print ("there are already many entries")
        break
    
  • Using XPATH:

    list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[contains(@id, 'doc_person_table') and contains(@id, 'person_rem_btn')]/span")))
    if not list:
        print ("there is no entry, you can proceed")
        # other steps
    elif len(list) == 1
        print ("there is only one entry, it will be removed to proceed")
        # other steps
    else:
        print ("there are already many entries")
        break
    

Upvotes: 0

Guy
Guy

Reputation: 50864

find_element_by_xpath doesn't return True or False, it returns WebElement or throws NoSuchElementException. You can use find_elements_by_xpath to get a list and check if this list contains any elements. Start with waiting for an unrelated element that can indicate the page is loaded

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[@id^='frmMain:doc_person_table")))
# assuming this element will always appear when the page is loaded

elements = driver.find_elements_by_xpath("//*[@id='frmMain:doc_person_table:1:person_rem_btn']/span[1]")
if elements: # more verbose if len(elements) > 0
    print ("there are already many entries")
    driver.close()
else:
    elements = driver.find_elements_by_xpath("//*[@id='frmMain:doc_person_table:0:person_rem_btn']/span[1]")
    if elements:
        print ("there is only one entry, it will be removed to proceed")
        elements[0].click()
    else:
        print ("there is no entry, you can proceed")

Upvotes: 1

Related Questions