While statement not evaluating to false for Selenium Webdriver
Question: I'm migrating a test I wrote in the Selenium IDE to Python WebDriver and I'm
having some issues with a **'while'** loop scenario. Here's the IDE code:

    
    
    while | selenium.isElementPresent("xpath=//select[@name='servers']/option")
       storeValue | //select[@name='servers']/option | myServerIP
       waitForElementPresent | name=servers
       addSelection | name=servers | ${myServerIP}
       waitForValue | //input[@value='Delete'] | Delete
       clickAndWait | //input[@value='Delete']
       waitForNotText | //select[@name='servers']/option | ${myServerIP}
    endWhile
    

I have a box that contains addresses of time servers that have been entered
(i.e. 129.6.15.30, time-d.nist.gov, etc..). While addresses are listed in the
server list box, the object **"//select[@name='servers']/option"** exists.
Once all the servers have been deleted, the object is no longer in existence.

While the server list object exists..

  * Stores the name of top server in the list.
  * Selects that server.
  * Deletes that server from the list
  * Confirms that server name has been removed from the list

As I'm trying to migrate this scenario over to WebDriver, I'm having some
issues.

    
    
    while expected_conditions.visibility_of_element_located("//select[@name='servers']/option"):
       myServerIP = driver.find_element_by_xpath("//select[@name='servers']/option").text
       assertExpectedConditionTrue(driver, "By.NAME", "servers")
       driver.find_element_by_xpath("//select[@name='servers']/option[contains(text(), '"+ myServerIP + "')]").text
       driver.find_element_by_xpath("//select[@name='servers']/option[contains(text(), '"+ myServerIP + "')]").click()
       assertExpectedValueConditionTrue(driver, "By.XPATH", "//input[@value='Delete']", "Delete")
       driver.find_element_by_xpath("//input[@value='Delete']").click()
       assertExpectedConditionFalse(driver, "By.XPATH", "//select[@name='servers']/option", myServerIP)
    

The server names are found and deleted just fine. However, the 'while' part
never seems to evaluate to being invisible, which causes a
**NoSuchElementException** failure (on the first line in the 'while' loop)
once all the server names are removed. I'm looking for a way to make the
'while' loop evaluate to false so it's exited gracefully once all the server
names have been removed.


Answer: Make a `while True` loop and exit the loop once you get a `TimeoutException`:

    
    
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    wait = WebDriverWait(driver, 10)
    while True:
        try:
            wait.until(EC.presence_of_element_located((By.XPATH, "//select[@name='servers']/option")))
        except TimeoutException:
            break
        # rest of the code
    

Or, alternatively, catch `NoSuchElementException`:

    
    
    from selenium.common.exceptions import NoSuchElementException
    
    while True:
        try:
            myServerIP = driver.find_element_by_xpath("//select[@name='servers']/option").text
        except NoSuchElementException:
            break
    

* * *

Additionally, `selenium` has this [`Select` class](https://selenium-
python.readthedocs.org/api.html#selenium.webdriver.support.select.Select)
which makes it easy to work with `select->option` HTML blocks:

    
    
    from selenium.webdriver.support.select import Select
    
    select = Select(driver.find_element_by_name("servers"))
    select_by_visible_text(myServerIP)
    

