While statement not evaluating to false for Selenium Webdriver While statement not evaluating to false for Selenium Webdriver selenium selenium

While statement not evaluating to false for Selenium Webdriver


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

from selenium.common.exceptions import TimeoutExceptionfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Bywait = 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 NoSuchElementExceptionwhile True:    try:        myServerIP = driver.find_element_by_xpath("//select[@name='servers']/option").text    except NoSuchElementException:        break

Additionally, selenium has this Select class which makes it easy to work with select->option HTML blocks:

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